List of models in Model in MVC

Posted by arri on Stack Overflow See other posts from Stack Overflow or by arri
Published on 2012-09-05T15:36:16Z Indexed on 2012/09/05 15:37 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I have two models:

class ModelIn{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
}

class ModelOut{
    public ModelOut(){ People = new List<ModelIn>();}
    public List<ModelIn> People { get; private set;}
    public string Country { get; set; }
}

And I have Controller editing ModelOut:

public ActionResult People()
{
    ...
    return View(SomeModelOutInstanceWith3People);
}
[HttpPost]
public ActionResult(ModelOut m)
{
    ...
}

In view I have sth like:

<% using (Html.BeginForm()) { %>
    <%: Html.EditorFor(m => Model.Country) %>
    <% for(int i = 0; i < Model.People.Count; ++i){ %>
        <%: Html.EditorFor(m => Model.People[i].FirstName) %>
        <%: Html.EditorFor(m => Model.People[i].LastName) %>
        <%: Html.EditorFor(m => Model.People[i].Address) %>
    <% } %>
    <input type="submit" />
<% } %>

It works all OK, but in post action I have empty ModelOut m. I can see in logs that data is sent correctly.

I have tried everything, nothing works.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET