ビューにコレクションを使用してモデルをバインドしているときに、次のインデクサー構文に遭遇しました。
ここに私が持っているものがあります:
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>
}
この[インデックス]の使用を避けるには? 他の正しい構文はありますか? それなしで@Html.DropDownListFor
は機能せず、ポストバックでモデルを更新することを見てください。