Html.Editor对于MVC4中的模型而不是属性

时间:2013-02-11 14:21:47

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有以下观点:

<fieldset>
        <legend>CreateCardViewModel</legend>

        <div class="editor-field">
            @Html.EditorFor(model => model.SetId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateCreated)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateCreated)
            @Html.ValidationMessageFor(model => model.DateCreated)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IsReady)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IsReady)
            @Html.ValidationMessageFor(model => model.IsReady)
        </div>

        @foreach (var side in Model.Sides)
        {
            @Html.EditorFor(model => model.Sides[side.SideId])
        }


        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

此片段:

@foreach (var side in Model.Sides)
            {
                @Html.EditorFor(model => model.Sides[side.SideId])
            }

显示Sides模型的所有属性的可编辑字段。定义哪个字段应该可以为此列表编辑的最合适方法是什么?

2 个答案:

答案 0 :(得分:1)

改为使用for循环:

@for (int i = 0; i < Model.Sides.Count; i++) {
   @Html.EditorFor(x => x.Sides[i])
}

答案 1 :(得分:0)

抓一点 - 我意识到我可以做到以下几点:

@for (int i = 0; i < Model.Sides.Count; i++)
        {
            @Html.EditorFor(x => x.Sides[i].Content)
            @Html.EditorFor(x => x.Sides[i].IsFront)
            @Html.EditorFor(x => x.Sides[i].Stage)
        }