How to Validate a Model in a jQuery Postback

半世苍凉 提交于 2019-12-11 20:11:28

问题


If I have a model;

Name
  [Required]
  FirstName
  [Required]
  LastName

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?

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

thanks


回答1:


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.

http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx



来源:https://stackoverflow.com/questions/3472810/how-to-validate-a-model-in-a-jquery-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!