Asp.net Validation just working for one property

2019-08-01 02:53发布

问题:

Following is my Model for Product

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
}

And following is my View code

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

Other than the Name property, validation isn't working.

回答1:

I found the answer to my problem. It's a bug or error in Asp.net MVC 3 as it is reported here : Unobtrusive Client Hooks Not Generated Via TextAreaFor For Nested Model Properties. This is the reason why the validations are aren't happening for ShortDescription in my case since i am using @Html.TextAreaFor.

Hope this is removed in MVC4