【问题标题】:Asp.net Validation just working for one propertyAsp.net 验证仅适用于一个属性
【发布时间】:2012-01-01 07:47:25
【问题描述】:

以下是我的产品模型

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
}

以下是我的查看代码

<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 属性,验证不起作用。

【问题讨论】:

  • 很好。你有问题吗?
  • LOL...是的,我有,这就是为什么其他验证(名称除外)不起作用的原因。当我点击保存而不在 ShortDescription 或长描述中输入任何内容时,不会显示无干扰的验证消息,并且表单会发回

标签: c# asp.net-mvc-3 unobtrusive-validation


【解决方案1】:

我找到了问题的答案。这是 Asp.net MVC 3 中的错误或错误,因为它在此处报告:Unobtrusive Client Hooks Not Generated Via TextAreaFor For Nested Model Properties。这就是为什么在我的情况下没有对 ShortDescription 进行验证的原因,因为我使用的是@Html.TextAreaFor

希望这在 MVC4 中被删除

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多