views:

45

answers:

4

Hello everybody,

How to bind a list to a controller?

Code in my controller, HTTPGET and HTTPPOST :

 public ActionResult Client(string id)
    {               
        List<Customer> Contacts = WebService.GetContactsOfClient(id);   
        return View(Contacts);
    }

    [HttpPost]
    public ActionResult Client(List<Customer> custs)
    {          
        // custs = null!!! Why??
        return View();

    }

Code in my view :

 <% using (Html.BeginForm("Client", "Formulaire", FormMethod.Post, new { id = "form" })) { %>
        <% foreach (var item in Model) { %>
            <%:item.MiseAjour %><br />
        <% } %>

        <input type="submit" />
<% } %>

Thank you all...

A: 

This outlines what you're trying to achieve.

http://stackoverflow.com/questions/2402434/model-bind-to-a-list-when-posting-javascript-data-object/2403100#2403100

If you have specific questions, post them

DaveDev
A: 

Your view is not rendering any html input elements which are required if you want the model binder to put your model back together on the post. See this answer here which shows binding to a collection and posting to back. Also check out this post for binding to a dynamic list.

Steve Michelotti
A: 

If you want to get some values back in your POST action you need to POST them which is achieved using input fields:

<% using (Html.BeginForm() { %>
    <%= Html.EditorForModel() %>
    <input type="submit" value="OK" />
<% } %>
Darin Dimitrov
A: 

Solution for me :

<legend>Contacts</legend>
      <% for (int i = 0; i < Model.Contacts.Count; i++) { %>

            <%: Html.EditorFor(model => model.Contacts[i],"Contact") %>

        <% } %>

With this, I bind my list to controller!

Thank you all!!

Akawan