开发者

Asp.net MVC 2.0 DataAnnotations Validation doesn't emit correct JSON

I'm trying to get setup using the DataAnnotations validator in ASP.Net MVC 2.0 Beta, but with the following model:

public class Foo {
    [Required] public string Bar {get;set;} 
}

And the following code in my view:

 <%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<Foo>" %>

 <!-- later on -->
 <% Html.EnableClientValidation(); %>
 <% using (Html.BeginForm("Edit","Foo")) { %>             

Everything is almost verbatim form the examples. What is emitted is:

<script type="text/javascript">
//<![CDATA[
EnableClientValidation({"Fields":[],"FormId":"form0"}, null);
//]]>
</script>

Nothing is ever emitted to tell 开发者_如何学JAVAwhatever JavaScript validation library (jQuery or MS Ajax, doesn't matter) to validate the fields. The validation does happen on the server side, but never on the client, for obvious reasons.


The answer is a bit sneaky. I found out the problem is the lack of ValidationMessageFor even though there is a ValidationSummary. After adding the following:

<%=Html.ValidationMessageFor(x=>x.Bar)%>

The proper JSON was emitted and the jQuery validation worked.

It seems odd you have to have the message even though you have the validation summary.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜