Asp.net MVC 2.0 DataAnnotations 验证不会发出正确的 JSON

发布于 2024-08-15 03:54:41 字数 733 浏览 7 评论 0原文

我正在尝试使用 ASP.Net MVC 2.0 Beta 中的 DataAnnotations 验证器进行设置,但使用以下模型:

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

在我看来,以下代码:

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

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

一切几乎都是逐字记录示例。发出的内容是:

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

不会发出任何内容来告诉任何 JavaScript 验证库(jQuery 或 MS Ajax,无关紧要)来验证字段。出于显而易见的原因,验证确实发生在服务器端,但从未发生在客户端。

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 whatever 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-08-22 03:54:41

答案有点偷偷摸摸。我发现问题是缺少 ValidationMessageFor,即使有 ValidationSummary。添加以下内容后:

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

发出了正确的 JSON 并且 jQuery 验证起作用了。

尽管您有验证摘要,但您仍然收到该消息,这似乎很奇怪。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文