0

我有一个由嵌套模型组成的表格,如下所示:

foreach (var item in Model)
   {
    <h3>
        @item.StageDescription
    </h3>
    <div class="well">
        <table id="Item@(item.ID)" class="WizardOption">
            <thead>
                <tr>
                    <some headings here />
                </tr>
            </thead>
            <tbody>
                @Html.EditorFor(m => item.WizardOptions,"","WizardOptions",null)
            </tbody>
        </table>
    </div>
}

WizardOption 类有一个必填字段调用显示值:

public class WizardOptionMetaData {
        [Required]
        public string DisplayValue { get; set; }
}

这适用于第一个表,如果我将 DisplayValue 字段留空,我会收到错误消息:“DisplayValue 字段是必需的。” 并呈现以下标记:

<input class="description-box" data-val="true" data-val-required="The DisplayValue field is required." id="WizardOptions_0__DisplayValue" name="WizardOptions[0].DisplayValue" type="text" value="">

但是第一个表之后的任何表都没有正确呈现验证:

<input class="description-box" id="WizardOptions_1__DisplayValue" name="WizardOptions[1].DisplayValue" type="text" value="">

我哪里错了?

4

1 回答 1

0

找到了一个问题的答案,直到我发布问题后才找到:

ASP.NET MVC 3:当 BeginForm 在布局上时生成不显眼的验证

@{
     var originalContext = ViewContext.FormContext;
     ViewContext.FormContext = new FormContext();
} 

<!-- This will generate proper HTML5 data-* validation attributes -->
@Html.TextBoxFor(x => x.Prop1)
     @Html.ValidationMessageFor(x => x.Prop1)

     @Html.TextBoxFor(x => x.Prop2)
     @Html.ValidationMessageFor(x => x.Prop2)

@{
     ViewContext.FormContext = originalContext;
 }
于 2012-05-09T08:46:39.823 回答