0

在将模型与集合绑定到视图期间,我遇到了下一个索引器语法。

这是我所拥有的:

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
}

我的观点:

@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>
}

如何避免这种[index]用法?还有其他正确的语法吗?看看,没有它就@Html.DropDownListFor无法工作并在回发时更新我的​​模型。

4

1 回答 1

1

您可以使用循环变量“客户”,如下所示:

@Html.DropDownListFor(x => customer.ImportAction)
于 2012-10-16T00:55:52.487 回答