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

时间:2021-07-26 20:14:46

I have the following view:

我有以下观点:

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

This snippet:

这个片段:

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

Displays editable fields for all properties of the Sides model. What is the most appropriate way to define which fields should be editable for this list?

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

2 个解决方案

#1


1  

Use a for loop instead:

改为使用for循环:

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

#2


0  

Scratch that - I realized I can do the following:

从头开始 - 我意识到我可以做到以下几点:

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

#1


1  

Use a for loop instead:

改为使用for循环:

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

#2


0  

Scratch that - I realized I can do the following:

从头开始 - 我意识到我可以做到以下几点:

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