Alexander Molodih
Alexander Molodih

Reputation: 1936

How can I return an array from view asp.net-mvc-3

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.

Upvotes: 2

Views: 2023

Answers (2)

BobTurbo
BobTurbo

Reputation: 240

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

Upvotes: 2

Zruty
Zruty

Reputation: 8687

The EditorFor() call is incorrect.

You should remove the foreach and do

...
@Html.EditorFor(modelItem => modelItem.Emails)
...

Upvotes: 3

Related Questions