在将模型与集合绑定到视图期间,我遇到了下一个索引器语法。
这是我所拥有的:
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
无法工作并在回发时更新我的模型。