0

In an ASP.NET MVC application, the model is a list of objects. It is used in the view like this:

<h4>Order Acknowledgement Contact(s)</h4>

@foreach (var contact in Model.Where(c => c.Type == "AK").Take(3))
{
    <div class="form-group">
        <div class="col-md-10">
            @Html.EditorFor(c => contact.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(c => contact.Email, "", new { @class = "text-danger" })
        </div>
    </div>
}

<h4>Shipping Acknowledgement Contact(s)</h4>

@foreach (var contact in Model.Where(c => c.Type == "BK").Take(3))
{
    <div class="form-group">
        <div class="col-md-10">
            @Html.EditorFor(c => contact.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(c => contact.Email, "", new { @class = "text-danger" })
        </div>
    </div>
}

When the form is submitted, in the controller the List is null.

    [HttpPost]
    public ActionResult Edit(List<CustomerContact> customerContacts)
    {

Is it possible to fix binding and preserve using linq etc?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • 2
    You could create a form per entry you wish to show, or you could keep a hidden index and post the entire list. – CodeCaster Nov 09 '21 at 20:44
  • @CodeCaster Everything should be posted at once. But every entry is identified by two values (Type and LineNumber), both are in CustomerContact, and Email is the value that is being edited. How can it be used? – David Shochet Nov 09 '21 at 20:51
  • As I use linq rather than for loop, how can I even have the index? – David Shochet Nov 09 '21 at 21:02
  • 1
    You need an index on your inputs to bind a posted collection https://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects – Jasen Nov 09 '21 at 21:17
  • I don't understand what you mean by "preserve using linq". – Jasen Nov 09 '21 at 21:18

1 Answers1

0

foreach loop never submits items back to the controller, it can only display items. So you should use only for loop.

So it is a good idea to create a viewmodel

public class ViewModel
{
public List<CustomerContact>  Aks {get; set;}
public List<CustomerContact>  Bks {get; set;}

{

your get action

var model =  new ViewModel
{
 Aks= contacts.Where(c => c.Type == "AK").Take(3));
 Bks= contacts.Where(c => c.Type == "BK").Take(3));
}
 ....
return View(model);

post action

    public ActionResult Edit(ViewModel model)

and finally view

@model ViewModel

.....
for (var i=0; i< Model.Aks; i++)
{
    <div class="form-group">
        <div class="col-md-10">
            @Html.EditorFor(model => model.Aks[i].Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Aks[i].Email, "", new { @class = "text-danger" })
        </div>
    </div>
}

.... the same for model.Bks
Serge
  • 40,935
  • 4
  • 18
  • 45