如何验证在一个jQuery回发模型 [英] How to Validate a Model in a jQuery Postback

查看:201
本文介绍了如何验证在一个jQuery回发模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个模型;

Name
  [Required]
  FirstName
  [Required]
  LastName

如果我创造我的jQuery的回传模式从而;

If I create the model in my jQuery postback thus;

Name name = new Name{ FirstName = param1, LastName = param2 };

有没有一种方法,我可以使用装饰领域的数据注释验证它?

Is there a way I can validate it using the data annotations that decorate the fields?

这是不是在回发事件的看法发生的事情,它是一个jQuery回传内发生

This is not happening in a postback event on the view, it's happening within a jQuery postback

感谢

推荐答案

我假设你正在发送数据到JSON格式的控制器。如果你是,你需要使用自定义JsonValueProvider不与模型验证侵入。一是为您提供在ASP.NET MVC期货组装和菲尔哈克写了一篇博客文章对如何在应用程序启动时注册的说明。

I am assuming that you are sending the data to the Controller in Json format. If you are, you need to use a custom JsonValueProvider that does not intrude with Model Validation. One is provided for you in the ASP.NET MVC Futures assembly and Phil Haack wrote a blog post with instructions on how to register it in your application startup.

<一个href=\"http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx\" rel=\"nofollow\">http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

这篇关于如何验证在一个jQuery回发模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆