简体   繁体   English

HTML.Editor对于MVC4中的模型而非属性

[英]Html.EditorFor a model, not a property, in MVC4

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. 显示Sides模型的所有属性的可编辑字段。 What is the most appropriate way to define which fields should be editable for this list? 定义此列表应可编辑哪些字段的最合适方法是什么?

Use a for loop instead: 请使用for循环:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM