2

I have view model:

    Customer
    {
        public string Name { get; set; }
        ...
        public IEnumerable<string> Emails { get; set; }
    }

I've post it in the view:

    ...
    @foreach (var Emails in Model.Emails)
    {
         @Html.EditorFor(modelItem => Emails)
    }
    ...

How can I return to controller an array of this Emails?

When I return data from form to controller in this moment, property "Customer.Emails" equals null, but it should contain an array of e-mails.

3
  • I removed my answer as you requested, but I still think it was correct. I'll test it and update when I have time Commented Aug 30, 2011 at 10:40
  • Undelete your question I mark it as answer, it was my fault. I was simplify my view and model in question for easier understanding. In this case it's works! In my real program I will think how to apply it. Thanks for answer. Commented Aug 30, 2011 at 10:58
  • I think you need to look into Editor Templates for ASP.NET MVC3: coding-in.net/asp-net-mvc-3-how-to-use-editortemplates Commented Aug 30, 2011 at 11:42

2 Answers 2

3

The EditorFor() call is incorrect.

You should remove the foreach and do

...
@Html.EditorFor(modelItem => modelItem.Emails)
...
Sign up to request clarification or add additional context in comments.

1 Comment

modelItem.Emails - it's an array. I ve try to put it, but it also cast array to string with all members in each edit.
2

You have to do

for (var i = 0; i < Model.Emails.Count; i++) {
    @Html.EditorFor(m => m.Emails[i]);
}

otherwise it won't generate the correct id for model binding (as you are not giving the EditorFor any context).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.