开发者

MVC 3.0 : why no client-side validation messages?

I have a model like this

public class SampleModel{
   public int No{get;set;} 
}

and in view I have a custom ValidationFor which shows a star near the inputbox for this field :

       @Html.EditorFor(model => model.No)
        @Html.ValidationMessageWithStarFor(model => model.No)

So in rendering I have a "*" nears the in开发者_如何学编程putbox which tells the field is required. It works in server-side properly but it doesn't work in client-side. In spite of Enabling ClientValidation in web.confing through these lines :

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="false" />

...how can I enable it?


You probably forgot to reference the following scripts in your page in addition to jQuery:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>


the problem was because of the following code :

 DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

which prevents dataannotation to validate not nullable types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜