locked
Pass List of objects from View to controller RRS feed

  • Question

  • User1506863308 posted

    Hi,

    I want to, pass the list of objects, from view to the controller's action.

    My aim is, i will display, the list of all product's details. The user can able edit any data (even all the data).

    So after editing, i want to post, entire list of the object, to controller's action, where i will decide something to continue on.

    My sample code is:-

    This is model:

     public ActionResult API_VM()
     {
    public List<XElement> Stoklar { get; set; }
    public HashSet<string> elemetnNames { get; set; }
    public string PaketAgirligi { get; set; }
    public string PaketGenisligi { get; set; }
    public string PaketUzunlgu { get; set; }
    public string PaketYuksekligi { get; set; }
    public string StokAded { get; set; }
    public string UrunAciklama { get; set; }
    public string Kategori { get; set; }
    public string UrunMarka { get; set; }
    public string UrunAdi { get; set; }
    public string UrunFiyat { get; set; }
    public string UrunDil { get; set; }
    public string SkuKodu { get; set; }
    public string ServisPolitikaNo { get; set; }
    public string KargoSablonID { get; set; }
    public string StokAzaltmaStrateji { get; set; }
    public string KargoyaVerilis { get; set; }
    public string UrunFoto1 { get; set; }
    public string UrunFoto2 { get; set; }
    public string UrunFoto3 { get; set; }
    public string UrunFoto4 { get; set; }
    public string UrunFoto5 { get; set; }
    public string UrunFoto6 { get; set; }
    public string ParentElement { get; set; }
    public string TedarikciLink { get; set; }
    }
    This is my post method:

    [HttpPost]
    public IActionResult UrunPost(API_VM[] model) *//Model is always nuLL
    { }
    This is my View:

    @model IList
    <API_VM> <body> <table> @using (@Html.BeginForm("UrunPost", "Post", FormMethod.Post)) { for (int i = 0; i < Model.Count; i++) { <tr> <td>Product Name:</td> <td>@Html.TextBox(model => Model[i].UrunAdi)</td> </tr> } <td><input type="submit" value="Post"></td> } </table> </body>
    Sunday, July 12, 2020 3:24 PM

All replies

  • User475983607 posted

    Model binding a collection is covered in the following blog.

    https://www.learnrazorpages.com/razor-pages/model-binding#binding-simple-collections

    The concept is fairly simple.  The element names must be indexed for the model binder to convert the post parameters into a C# collection.  

    Sunday, July 12, 2020 3:39 PM
  • User1506863308 posted

    mgebhard

    Model binding a collection is covered in the following blog.

    https://www.learnrazorpages.com/razor-pages/model-binding#binding-simple-collections

    The concept is fairly simple.  The element names must be indexed for the model binder to convert the post parameters into a C# collection.  

    Can u show me a sample code pease?

    Sunday, July 12, 2020 3:56 PM
  • User1506863308 posted

    mgebhard

    Model binding a collection is covered in the following blog.

    https://www.learnrazorpages.com/razor-pages/model-binding#binding-simple-collections

    The concept is fairly simple.  The element names must be indexed for the model binder to convert the post parameters into a C# collection.  

    Still doesn't work. Model always null.

    public class API_VM
        {
            public API_VM()
            {
                elemetnNames = new HashSet<string>();
            }
            public List<XElement> Stoklar { get; set; }
            public HashSet<string> elemetnNames { get; set; }
            public string PaketAgirligi { get; set; }
            public string PaketGenisligi { get; set; }
            public string PaketUzunlgu { get; set; }
            public string PaketYuksekligi { get; set; }
            public string StokAded { get; set; }
            public string UrunAciklama { get; set; }
            public string Kategori { get; set; }
            public string UrunMarka { get; set; }
            public string UrunAdi { get; set; }
            public string UrunFiyat { get; set; }
            public string UrunDil { get; set; }
            public string SkuKodu { get; set; }
            public string ServisPolitikaNo { get; set; }
            public string KargoSablonID { get; set; }
            public string StokAzaltmaStrateji { get; set; }
            public string KargoyaVerilis { get; set; }
            public string UrunFoto1 { get; set; }
            public string UrunFoto2 { get; set; }
            public string UrunFoto3 { get; set; }
            public string UrunFoto4 { get; set; }
            public string UrunFoto5 { get; set; }
            public string UrunFoto6 { get; set; }
            public string ParentElement { get; set; }
            public string TedarikciLink { get; set; }
    
    
        }
    public class API_List:PageModel
        {
            [BindProperty]
            public List<API_VM> ApiList { get; set; }
        }


    @model API_List
    @{
        ViewData["Title"] = "UrunlerListe";
        Layout = "~/Views/Shared/_Layout.cshtml";
        int i = 0;
    
    }
        <<form class="form-horizontal" method="post" asp-action="UrunPost">
        <table class="table table-striped">
            <tr>
                <th>Product Name</th>
            </tr>
            @foreach (var item in Model.ApiList)
            {
                <tr>
                    <td>
                        <input type="text" asp-for="ApiList[i].UrunAdi" value="@item.UrunAdi" />
                    </td>
                </tr>
            }
        </table>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </div>
    </form>

    [HttpPost]
           public IActionResult UrunPost(API_List model) */ Model is always null.
           {
    
           }
     
    Sunday, July 12, 2020 6:07 PM
  • User-474980206 posted

    you didi not use the correct binding name. you to use the real index value (and a razor expression "@" prefix). try:

            @for (var i=0; i < Model.ApiList.Count; ++i)
            {
                <tr>
                    <td>
                        <input type="text" asp-for="@Model.ApiList[i].UrunAdi" />
                    </td>
                </tr>
            }

    as you are only passing one property on postback, it will be the only one filled in, the rest will be null

    Sunday, July 12, 2020 8:25 PM
  • User1506863308 posted

    bruce (sqlwork.com)

    you didi not use the correct binding name. you to use the real index value (and a razor expression "@" prefix). try:

            @for (var i=0; i < Model.ApiList.Count; ++i)
            {
                <tr>
                    <td>
                        <input type="text" asp-for="@Model.ApiList[i].UrunAdi" />
                    </td>
                </tr>
            }

    as you are only passing one property on postback, it will be the only one filled in, the rest will be null

    Still it doesnt work. Null again. Should I fill all properties?

    Sunday, July 12, 2020 9:06 PM
  • User2078676645 posted

    Hi,

    This is the content of the Razor page. But in the beginning the problem you had was the Razor view.

    public class API_List:PageModel
        {
            [BindProperty]
            public List<API_VM> ApiList { get; set; }
        }

    If you use the Razor view, in this way, data can be successfully transferred to the action. Please check your controller name.

    View

    @model IList<API_VM>
    <body>
        <table>
            @using (@Html.BeginForm("UrunPost", "Post", FormMethod.Post))
            {
                for (int i = 0; i < Model.Count; i++)
                {
                    <tr>
                        <td>Product Name:</td>
                        <td><input type="text" asp-for="@Model[i].UrunAdi" /></td>
                    </tr>
    
                }
                <td><input type="submit" value="Post"></td>
            }
        </table>
    </body>

    Controller

    [HttpPost]
            public IActionResult UrunPost(API_VM[] model) 
            {
                return Json(model);
            }

    Model

    public class API_VM
        {
            public List<XElement> Stoklar { get; set; }
            public HashSet<string> elemetnNames { get; set; }
            public string PaketAgirligi { get; set; }
            public string PaketGenisligi { get; set; }
            public string PaketUzunlgu { get; set; }
            public string PaketYuksekligi { get; set; }
            public string StokAded { get; set; }
            public string UrunAciklama { get; set; }
            public string Kategori { get; set; }
            public string UrunMarka { get; set; }
            public string UrunAdi { get; set; }
            public string UrunFiyat { get; set; }
            public string UrunDil { get; set; }
            public string SkuKodu { get; set; }
            public string ServisPolitikaNo { get; set; }
            public string KargoSablonID { get; set; }
            public string StokAzaltmaStrateji { get; set; }
            public string KargoyaVerilis { get; set; }
            public string UrunFoto1 { get; set; }
            public string UrunFoto2 { get; set; }
            public string UrunFoto3 { get; set; }
            public string UrunFoto4 { get; set; }
            public string UrunFoto5 { get; set; }
            public string UrunFoto6 { get; set; }
            public string ParentElement { get; set; }
            public string TedarikciLink { get; set; }
        }

    Best Regards,

    Evern

    Monday, July 13, 2020 3:05 AM
  • User1506863308 posted

    Hi,

    This is the content of the Razor page. But in the beginning the problem you had was the Razor view.

    public class API_List:PageModel
        {
            [BindProperty]
            public List<API_VM> ApiList { get; set; }
        }

    If you use the Razor view, in this way, data can be successfully transferred to the action. Please check your controller name.

    View

    @model IList<API_VM>
    <body>
        <table>
            @using (@Html.BeginForm("UrunPost", "Post", FormMethod.Post))
            {
                for (int i = 0; i < Model.Count; i++)
                {
                    <tr>
                        <td>Product Name:</td>
                        <td><input type="text" asp-for="@Model[i].UrunAdi" /></td>
                    </tr>
    
                }
                <td><input type="submit" value="Post"></td>
            }
        </table>
    </body>

    Controller

    [HttpPost]
            public IActionResult UrunPost(API_VM[] model) 
            {
                return Json(model);
            }

    Model

    public class API_VM
        {
            public List<XElement> Stoklar { get; set; }
            public HashSet<string> elemetnNames { get; set; }
            public string PaketAgirligi { get; set; }
            public string PaketGenisligi { get; set; }
            public string PaketUzunlgu { get; set; }
            public string PaketYuksekligi { get; set; }
            public string StokAded { get; set; }
            public string UrunAciklama { get; set; }
            public string Kategori { get; set; }
            public string UrunMarka { get; set; }
            public string UrunAdi { get; set; }
            public string UrunFiyat { get; set; }
            public string UrunDil { get; set; }
            public string SkuKodu { get; set; }
            public string ServisPolitikaNo { get; set; }
            public string KargoSablonID { get; set; }
            public string StokAzaltmaStrateji { get; set; }
            public string KargoyaVerilis { get; set; }
            public string UrunFoto1 { get; set; }
            public string UrunFoto2 { get; set; }
            public string UrunFoto3 { get; set; }
            public string UrunFoto4 { get; set; }
            public string UrunFoto5 { get; set; }
            public string UrunFoto6 { get; set; }
            public string ParentElement { get; set; }
            public string TedarikciLink { get; set; }
        }

    Best Regards,

    Evern

    Still null. I'm gonna be crazy. Why it doesn't work I dont understand.

    Monday, July 13, 2020 1:10 PM
  • User475983607 posted

    The screenshot is a compiler warning telling you that you defined an input parameter but the input parameter is never used which has nothing to do with the original issue.

    I create a very basic test that illustrates the pattern for submitting a collection.   However, I think you have a misunderstanding because the view model has many properties but the view only posts one of the properties.   I'm guessing you expect the View Model to persist between the GET and the POST which is not what happens.  

    @model IList<API_VM>
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    @using (@Html.BeginForm("UrunPost", "General", FormMethod.Post))
    {
        <table>
    
            @for (int i = 0; i < Model.Count; i++)
            {
            <tr>
                <td>Product Name:</td>
                <td><input type="text" asp-for="@Model[i].UrunAdi" /></td>
            </tr>
    
            }
        </table>
    
        <div>
            <input type="submit" value="Post">
        </div>
    }
    public class GeneralController : Controller
        {
            // GET: General
            public ActionResult Index()
            {
                List<API_VM> vm = PopulateVm();
                return View(vm);
            }
    
    
            [HttpPost]
            public IActionResult UrunPost(API_VM[] model)
            {
                return Ok(model);
            }
    
            private List<API_VM> PopulateVm()
            {
                return new List<API_VM>()
                {
                    new API_VM()
                    {
                        UrunAdi = "Hello",
                        Kategori = "This is a test"
                    },
                    new API_VM()
                    {
                        UrunAdi = "World",
                        Kategori = "This is a test"
                    }
                };
            }
        }



    Results

    [
      {
        "stoklar": null,
        "elemetnNames": [
          
        ],
        "paketAgirligi": null,
        "paketGenisligi": null,
        "paketUzunlgu": null,
        "paketYuksekligi": null,
        "stokAded": null,
        "urunAciklama": null,
        "kategori": null,
        "urunMarka": null,
        "urunAdi": "Hello",
        "urunFiyat": null,
        "urunDil": null,
        "skuKodu": null,
        "servisPolitikaNo": null,
        "kargoSablonID": null,
        "stokAzaltmaStrateji": null,
        "kargoyaVerilis": null,
        "urunFoto1": null,
        "urunFoto2": null,
        "urunFoto3": null,
        "urunFoto4": null,
        "urunFoto5": null,
        "urunFoto6": null,
        "parentElement": null,
        "tedarikciLink": null
      },
      {
        "stoklar": null,
        "elemetnNames": [
          
        ],
        "paketAgirligi": null,
        "paketGenisligi": null,
        "paketUzunlgu": null,
        "paketYuksekligi": null,
        "stokAded": null,
        "urunAciklama": null,
        "kategori": null,
        "urunMarka": null,
        "urunAdi": "World",
        "urunFiyat": null,
        "urunDil": null,
        "skuKodu": null,
        "servisPolitikaNo": null,
        "kargoSablonID": null,
        "stokAzaltmaStrateji": null,
        "kargoyaVerilis": null,
        "urunFoto1": null,
        "urunFoto2": null,
        "urunFoto3": null,
        "urunFoto4": null,
        "urunFoto5": null,
        "urunFoto6": null,
        "parentElement": null,
        "tedarikciLink": null
      }
    ]

    Monday, July 13, 2020 2:09 PM