1

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 to edit any data (even all the data).

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

My sample code is:-

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.
   {

   }
2
  • 1
    It is null because you are using a List on frontend while only taking one object in controller. you have to assign the "name" html attribute. Commented Jul 13, 2020 at 7:37
  • You're using a foreach but then also an i you never defined. I suspect you were intending to use a for but didn't. Commented Jul 13, 2020 at 10:25

1 Answer 1

1

Model binding by using @​​​​​​​model List<API_VM> rather than @model API_List.

@model List<API_VM>

@{
    ViewData["Title"] = "UrunlerListe";
    Layout = null;

}
<form class="form-horizontal" method="post" asp-action="UrunPost">
    <table class="table table-striped">
        <tr>
            <th>Product Name</th>
        </tr>
        
            @for (int i = 0; i < Model.Count; i++)
            { 
                <tr>
                    <td>
                    <input type="text" asp-for="@Model[i].UrunAdi" value="@Model[i].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>

In controller, you can use List<API_VM>.

    [HttpPost]
    public ActionResult UrunPost(List<API_VM> model)
    {

        API_List list = new API_List();
        list.ApiList = model;


        return View("~/Views/API_VMs/Index.cshtml", list);
    }

Screenshots of test:

enter image description here

UPDATE


You can use IFormCollection to check the difference of return type from FOREACH and FOR loop.

    [HttpPost]
    public string UrunPost(IFormCollection collection)
    {
       //
    }

And here is another solution which can also parse the type but not recommend if the class have too much attribute to cast.

    [HttpPost]
    public ActionResult UrunPost(IFormCollection collection)
    {
        var apivms = FormCollectionToAPIVM(collection);

        return View("~/Views/API_VMs/Index.cshtml", apivms);
    }

    private API_List FormCollectionToAPIVM(IFormCollection formCollection)
    {
        API_List apis = new API_List();

        for (var i = 0; i < formCollection.Count; i++)
        {
            string key = $"ApiList[{i}].UrunAdi";

            if (formCollection.ContainsKey(key)) { 
                
                var item = new API_VM();
                item.UrunAdi = formCollection[key];

                apis.ApiList.Add(item);
            }
        }

        return apis;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm doing the same but still null. I'm gonna be crazy. I have been trying for 3 days and I can't do that.
Hi, @Baran Polat,I'm sorry I didn't remind you can't use FOREACH instead of using for loop. Only use @Model[i], then you can get list array. That's why you got null here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.