0

我正在做一个 MVC 3 应用程序。我有一个模型[Range(1, 175, ErrorMessage="Invalid")]

在其中一个控制器上,视图完美呈现所有标记以进行验证。在具有相同设置的第二个控制器上,在下拉列表中完成的范围验证不会出现在 html 标记上。我有验证和 unostrusiveValidation true on config.web。我正在使用LINQTOSQL并且我已经完成了一个部分类来添加额外的元数据。该字段确实选择了[Display(Name="State")],但 Range 不是。

 <tr>
            <td>@Html.LabelFor(x => x.carta.INVprovincia)</td>
            <td>@Html.DropDownListFor(x => x.carta.INVprovincia, Model.provinciaItems, new { @class = "ddlsmall" }) <br /> @Html.ValidationMessageFor(x => x.carta.INVprovincia)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(x => x.carta.INVmunicipio)</td>
            <td>@Html.DropDownListFor(x => x.carta.INVmunicipio, Model.municipiosItems, new { @class = "ddlsmall" }) <br /> @Html.ValidationMessageFor(x => x.carta.INVmunicipio)</td>
        </tr>
4

1 回答 1

1

The Html.XXX helpers won't generate HTML5 data-* attributes used by the unobtrusive validation framework if they are not inside a form which seems to be your case. I guess that the form is contained within a parent view. This bug (IMHO) is fixed in ASP.NET MVC 4. A possible workaround is to put the following on the to of your partial view to fake a form and make the helpers believe that they are inside a form:

@{
    ViewContext.FormContext = new FormContext();
}
于 2012-08-21T16:12:09.387 回答