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

この[インデックス]の使用を避けるには? 他の正しい構文はありますか? それなしで@Html.DropDownListForは機能せず、ポストバックでモデルを更新することを見てください。

4

1 に答える 1

1

次のようにループ変数 'c​​ustomer' を使用できます。

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