Asp.net验证只适用于一个属性

本文关键字:一个 属性 net 验证 适用于 Asp | 更新日期: 2023-09-27 18:19:14

以下是我的产品模型

public class Product
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Please Enter Product Name")]
    [StringLength(100)]
    public string Name { get; set; }
    [Required(ErrorMessage = "Please Enter Short Desciption")]
    [StringLength(200)]
    .   // other properties
    .   // Removed for brevity
}

下面是View代码

<div class="contentHolder">
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
       Html.Telerik().TabStrip()
           .Name("TabStrip")
           .Items(tabstrip =>
           {
               tabstrip.Add()
                   .Text("General")
                   .ContentHtmlAttributes(new { style = "height:700px" })
                   .Content(@<text>
                <table>
                    <tr>
                        <td class="editor-label">
                            @Html.LabelFor(model => model.Product.Name)
                        </td>
                        <td class="editor-field">
                            @Html.EditorFor(model => model.Product.Name)
                            @Html.ValidationMessageFor(model => model.Product.Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="editor-label">
                            @Html.LabelFor(model => model.Product.ShortDescription)
                        </td>
                        <td class="editor-field">
                            @Html.TextAreaFor(model => model.Product.ShortDescription, new { cols = "50%", rows = "3" })
                            @Html.ValidationMessageFor(model => model.Product.ShortDescription)
                        </td>
                    </tr>
                </table>
                </text>);
           })
           .SelectedIndex(0)
           .Render();
}

Name属性外,验证不起作用

Asp.net验证只适用于一个属性

我找到了问题的答案。这是Asp.net MVC 3中的一个bug或错误,在这里报告:对于嵌套模型属性,不显眼的客户端钩子不是通过TextAreaFor生成的。这就是为什么在我使用@Html.TextAreaFor的情况下,验证没有发生在ShortDescription的原因。

希望在MVC4中删除