Does ASP.NET MVC provide a standard validator functionality or do you have to create your own validation manually? If the latter, is there any third party validator available that you can use on ASP.NET MVC web applications?
ASP.NET MVC是否提供标准验证器功能,或者您是否必须手动创建自己的验证?如果是后者,是否有可用于ASP.NET MVC Web应用程序的第三方验证器?
5 个解决方案
#1
Shortly after I posted this answer I found xval which is a validation framework for ASP.NET MVC.
在我发布这个答案后不久,我发现xval是ASP.NET MVC的验证框架。
#2
ASP.NET MVC contains methods like Html.ValidationSummary()
and Html.ValidationMessage()
. These are updated automatically if you use TryUpdateModel
. You could also validate manually and set the errormessages yourself. Here is an example of how use it.
ASP.NET MVC包含Html.ValidationSummary()和Html.ValidationMessage()等方法。如果您使用TryUpdateModel,这些会自动更新。您也可以手动验证并自己设置错误消息。这是一个如何使用它的例子。
#3
David Hayden wrote an article over at www.codebetter.com describing a great way to handle validation. Of course xVal is an option but it's always great to have an understanding.
David Hayden在www.codebetter.com上写了一篇文章,描述了一种处理验证的好方法。当然xVal是一个选项,但总是很了解。
#4
I implemented a variant of the code I found on Stephen Walther's blog. I use it with LINQ2SQL models by defining an IValidatedEntity interface that includes the GetRuleViolations() method and implementing the partial OnValidate method that calls GetRuleViolations() and throws a custom exception if the number of violations is non-zero. In the controller, this fires on SubmitChanges for the data context. If I get an exception I requery the model via the GetRuleViolations() method to build model errors to pass back to the view.
我实现了我在Stephen Walther博客上找到的代码变体。我通过定义一个IValidatedEntity接口来使用LINQ2SQL模型,该接口包含GetRuleViolations()方法并实现调用GetRuleViolations()的部分OnValidate方法,并在违规次数非零时抛出自定义异常。在控制器中,这会在SubmitChanges上触发数据上下文。如果我得到一个异常,我通过GetRuleViolations()方法重新查询模型,以构建模型错误以传递回视图。
#5
You can also checkout the .net validation framework. Its a rules framework that lets you create validators, apply the validators to rules, attach rules to your model, and check those rules at runtime on both the client and server. It provides flexible ways to configure rules - making heavy use of linq for both fluent and strongly typed configuration. It also provides extensibility points to create your own client script generators and rules.
您还可以签出.net验证框架。它是一个规则框架,允许您创建验证器,将验证器应用于规则,将规则附加到模型,并在运行时在客户端和服务器上检查这些规则。它提供了灵活的配置规则的方法 - 大量使用linq用于流畅和强类型配置。它还提供了可扩展性点,以创建您自己的客户端脚本生成器和规则。
The framework leverages the MVC RC HtmlHelpers and default conventions.
该框架利用MVC RC HtmlHelpers和默认约定。
If you download the latest source you can see an example of the framework working in the SplitBranch -> QSAspMvc quickstart project. Its still being actively developed.
如果您下载最新的源代码,您可以在SplitBranch - > QSAspMvc快速入门项目中看到该框架的示例。它仍然在积极发展。
#1
Shortly after I posted this answer I found xval which is a validation framework for ASP.NET MVC.
在我发布这个答案后不久,我发现xval是ASP.NET MVC的验证框架。
#2
ASP.NET MVC contains methods like Html.ValidationSummary()
and Html.ValidationMessage()
. These are updated automatically if you use TryUpdateModel
. You could also validate manually and set the errormessages yourself. Here is an example of how use it.
ASP.NET MVC包含Html.ValidationSummary()和Html.ValidationMessage()等方法。如果您使用TryUpdateModel,这些会自动更新。您也可以手动验证并自己设置错误消息。这是一个如何使用它的例子。
#3
David Hayden wrote an article over at www.codebetter.com describing a great way to handle validation. Of course xVal is an option but it's always great to have an understanding.
David Hayden在www.codebetter.com上写了一篇文章,描述了一种处理验证的好方法。当然xVal是一个选项,但总是很了解。
#4
I implemented a variant of the code I found on Stephen Walther's blog. I use it with LINQ2SQL models by defining an IValidatedEntity interface that includes the GetRuleViolations() method and implementing the partial OnValidate method that calls GetRuleViolations() and throws a custom exception if the number of violations is non-zero. In the controller, this fires on SubmitChanges for the data context. If I get an exception I requery the model via the GetRuleViolations() method to build model errors to pass back to the view.
我实现了我在Stephen Walther博客上找到的代码变体。我通过定义一个IValidatedEntity接口来使用LINQ2SQL模型,该接口包含GetRuleViolations()方法并实现调用GetRuleViolations()的部分OnValidate方法,并在违规次数非零时抛出自定义异常。在控制器中,这会在SubmitChanges上触发数据上下文。如果我得到一个异常,我通过GetRuleViolations()方法重新查询模型,以构建模型错误以传递回视图。
#5
You can also checkout the .net validation framework. Its a rules framework that lets you create validators, apply the validators to rules, attach rules to your model, and check those rules at runtime on both the client and server. It provides flexible ways to configure rules - making heavy use of linq for both fluent and strongly typed configuration. It also provides extensibility points to create your own client script generators and rules.
您还可以签出.net验证框架。它是一个规则框架,允许您创建验证器,将验证器应用于规则,将规则附加到模型,并在运行时在客户端和服务器上检查这些规则。它提供了灵活的配置规则的方法 - 大量使用linq用于流畅和强类型配置。它还提供了可扩展性点,以创建您自己的客户端脚本生成器和规则。
The framework leverages the MVC RC HtmlHelpers and default conventions.
该框架利用MVC RC HtmlHelpers和默认约定。
If you download the latest source you can see an example of the framework working in the SplitBranch -> QSAspMvc quickstart project. Its still being actively developed.
如果您下载最新的源代码,您可以在SplitBranch - > QSAspMvc快速入门项目中看到该框架的示例。它仍然在积极发展。