C# 使用 ASP.NET MVC 实现字段验证的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to implement field validation using ASP.NET MVC?
提问by samiq
I am building a public website using ASP.NET, as part of the deliverable I need to do an Admin Site for data entry of the stuff shown in the public site, I was wondering what techniques or procedures are people using to validate entries using ASP.NET MVC.
我正在使用 ASP.NET 构建一个公共网站,作为可交付成果的一部分,我需要做一个管理站点来输入公共站点中显示的内容,我想知道人们使用哪些技术或程序来验证使用 ASP 的条目.NET MVC。
采纳答案by Daniel Pollard
Take a look at the JQuery Validation pluginthis plugin is amazing,it's clean to implement and has all the features you could ever need, including remote validation via AJAX.
看看JQuery Validation 插件,这个插件很棒,它实现起来很干净,并且拥有您可能需要的所有功能,包括通过 AJAX 进行远程验证。
Also a sample MVC controller method can be found herewhich basically uses the JsonResult action type like:
还可以在此处找到一个示例 MVC 控制器方法,它基本上使用 JsonResult 操作类型,例如:
public JsonResult CheckUserName(string username)
{
return Json(CheckValidUsername(username));
}
回答by Emad
My favorite way it perform both client and server validation using model-based attributes. I wrote a short post about this and released the source code as well, that will basically allow you to create a class like this
我最喜欢的方式是使用基于模型的属性执行客户端和服务器验证。我写了一篇关于这个的简短文章并发布了源代码,这基本上可以让你创建一个这样的类
class User {
[Required]
public string Name{get;set;}
[Email][Required]
public string Email {get;set;}
}
And the appropriate javascript code will be generated to perform client validation as well as server-side validation runner will be validate your submitted form.
并且将生成适当的 javascript 代码来执行客户端验证以及服务器端验证运行器将验证您提交的表单。
Read the post over here
在这里阅读帖子
回答by Adrian Grigore
IMO using xVal with jQuery and DataAnnotationsModelBinder is the best combination.
IMO 将 xVal 与 jQuery 和 DataAnnotationsModelBinder 结合使用是最佳组合。
Sometimes however, there are validation rules which cannot be checked entirely on the client side, so you need to use remote client-side validation.
但是,有时有些验证规则无法完全在客户端检查,因此您需要使用远程客户端验证。
I figured out way to generically implement remote client-side validation with xVal / jQuery.validate so that
我想出了使用 xVal / jQuery.validate 一般实现远程客户端验证的方法,以便
- Validation rules remain solely in your ASP.NET MVC model
- You write each validation rule just once, and only in easily testable C# code. There is no JavaScript or other client-side counterpart .
- There is no need to branch or otherwise modify xVal or jquery.validate
- All you have to do for each new remote form validation rule is to derive from the base class shown in this article.
- 验证规则仅保留在您的 ASP.NET MVC 模型中
- 每个验证规则只需编写一次,并且只能使用易于测试的 C# 代码。没有 JavaScript 或其他客户端对应物。
- 无需分支或以其他方式修改 xVal 或 jquery.validate
- 对于每个新的远程表单验证规则,您所要做的就是从本文中显示的基类派生。
I wrote a blog articleon this describing all the details.