0

I encountered the next indexer syntax during binding my model with collection to view.

Here is what I have:

public class CustomerModel
{
    public List<Customer> Customers { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ImportAction ImportAction { get; set; }
}

public enum ImportAction
{
    Skip,
    Add,
    Merge
}

My view:

@using (Html.BeginForm("Edit", "Home"))
{
    var index = 0;
    foreach (var customer in Model.Customers)
    {
        <span>Name: @customer.Name</span>
        @Html.DropDownListFor(x => x.Customers[index].ImportAction, customer.ImportAction.ToListItems())
        index++;
    }
    <button type="submit">
        Submit</button>
}

How to avoid this [index] usage? Any other correct syntax? Take to the look, that without it @Html.DropDownListFor would not work and update my model on post back.

0

1 Answer 1

1

you can use the loop variable 'customer' like the following:

@Html.DropDownListFor(x => customer.ImportAction)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.