简体   繁体   English

验证ASP.NET MVC框架中的已发布表单数据

[英]Validating posted form data in the ASP.NET MVC framework

I've been playing around with the ASP.NET MVC Framework and the one thing that's really confusing me is how I'm meant to do server side validation of posted form data. 我一直在玩ASP.NET MVC框架,让我感到困惑的一件事就是我打算如何对发布的表单数据进行服务器端验证。 I presume I don't post back to the same URL, but if I don't, how do I redisplay the form with the entered data and error messages? 我认为我不会回发到同一个网址,但如果不这样做,我如何使用输入的数据和错误消息重新显示表单? Also, where should the validation logic go? 此外,验证逻辑应该在哪里? In the model or the controller? 在模型或控制器? This seems to be one of the few areas where web forms are much stronger (I miss the validation controls). 这似乎是Web表单更强大的少数领域之一(我错过了验证控件)。

Here's an overview of the flow in MVC: 以下是MVC中流程的概述:

  1. /new - render your "New" view containing a form for the user to fill out / new - 渲染包含用户填写表单的“新”视图
    • User fills out form and it is posted to /create 用户填写表单并将其发布到/ create
    • The post is routed to the Create action on your controller 帖子将路由到控制器上的“创建”操作
    • In your action method, update the model with the data that was posted. 在您的操作方法中,使用已发布的数据更新模型。
    • Your Model should validate itself. 您的模型应该验证自己。
    • Your Controller should read if the model is valid. 如果模型有效,您的控制器应该读取。
    • If the Model is valid, save it to your db. 如果模型有效,请将其保存到数据库。 Redirect to /show to render the show View for your object. 重定向到/ show以呈现对象的show View。
    • If the Model is invalid, save the form values and error messages in the TempData, and redirect to the New action again. 如果模型无效,请将表单值和错误消息保存在TempData中,然后再次重定向到“新建”操作。 Fill your form fields with the data from TempData and show the error message(s). 使用TempData中的数据填写表单字段并显示错误消息。

The validation frameworks will help you along in this process. 验证框架将帮助您完成此过程。 Also, I think the ASP.NET MVC team is planning a validation framework for the next preview. 此外,我认为ASP.NET MVC团队正在为下一个预览计划验证框架。

You might want to take a look at ScottGu's latest post for ASP.Net prev 5. It walks through a validation sample that is very interesting: 您可能想看一下ScottGu关于ASP.Net上一页的最新帖子。它将介绍一个非常有趣的验证示例:

http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx

As far as I can tell everyone is still trying to figure out the "standard" way of doing it. 据我所知,每个人都在试图找出“标准”的做法。 That said definitely check out Phil Haack and Scott Guthrie's latest posts on MVC and you'll find some interesting info on how they did. 那肯定是看看Phil Haack和Scott Guthrie关于MVC的最新帖子,你会发现一些关于他们如何做的有趣信息。 When I was just playing around with it for myself I created a ModelBinder for the LinqToSql data class that I had generated. 当我为自己玩它时,我为我生成的LinqToSql数据类创建了一个ModelBinder。 You can check out this post to find out how to put together a basic ModelBinder: 你可以查看这篇文章,了解如何组建一个基本的ModelBinder:

ASP.Net MVC Model Binder ASP.Net MVC Model Binder

The in your action if you had created a "Product" ModelBinder you would just declare the action like so: 在您的操作中,如果您创建了“Product”ModelBinder,您只需声明操作:

public ActionResult New(Product prod) 公共ActionResult新产品(产品产品)

And the model binder will take care of assigning posted data to the objects properties as long as you've built it right anyway. 并且模型绑定器将负责将发布的数据分配给对象属性,只要您无论如何都正确构建它。

After that within your GetValue() method you can implement whatever validation you want, whether using exception's, regex's, or whatever you can make a call like: 在你的GetValue()方法中,你可以实现你想要的任何验证,无论是使用异常,正则表达式,还是你可以调用的任何东西,如:

(ModelStateDictionary_name).AddModelError("form_element_id", "entered_value", "error_message"); (ModelStateDictionary_name).AddModelError(“form_element_id”,“entered_value”,“error_message”);

Then you can just throw a <%= Html.ValidationSummary() %> in your view to display all your errors. 然后你可以在视图中抛出一个<%= Html.ValidationSummary()%>来显示你的所有错误。

For client-side validation I just used jQuery. 对于客户端验证,我只使用了jQuery。 After you get a basic sample set up though you can start doing some interesting things combining all that with Partial Views and Ajax calls. 在获得基本样本设置之后,您可以开始做一些有趣的事情,将所有这些与部分视图和Ajax调用相结合。

Have you taken a look at this? 你看过这个吗? http://www.codeplex.com/MvcValidatorToolkit http://www.codeplex.com/MvcValidatorToolkit

Quoted from the page 引自该页面

The Validator Toolkit provides a set of validators for the new ASP.NET MVC framework to validate HTML forms on the client and server-side using validation sets. Validator Toolkit为新的ASP.NET MVC框架提供了一组验证器,以使用验证集在客户端和服务器端验证HTML表单。

I'm afraid that someone more MVC-savvy than me would have to speak to where in the architecture you should put things. 我担心,比我更精通MVC的人不得不谈论你应该把建筑物放在哪里。

I'm just learning the MVC framework too so I'm not sure how off this is, but from what I understand you would have a form on a View such as Edit.aspx. 我只是学习MVC框架,所以我不确定它是多么偏离,但据我所知,你会在View.aspx这样的View上有一个表单。 This form would then post to the controller to another action method such as Update() passing in the contents of the form that you set in Edit.aspx as parameters. 然后,此表单将向控制器发送另一个操作方法,例如Update()传递您在Edit.aspx中设置的表单内容作为参数。

Update(int id, string name, string foo)

You could do the validation within that method. 您可以在该方法中进行验证。 If all is ok, 如果一切正常,

return View("Item", yourObject)

There is Castle.Components.Validator module in Castle project. Castle项目中有Castle.Components.Validator模块。 It's very agile and powerfull. 它非常敏捷和强大。 It generates validation rules based on model attributes (or any other source) and even able to generate JS validation using jQuery, Prototype Validation, fValidate and other. 它基于模型属性(或任何其他源)生成验证规则,甚至能够使用jQuery,Prototype Validation,fValidate等生成JS验证。 Of course it's wise to abstract validator away behind IValidationEngine interface. 当然,在IValidationEngine接口后面抽象验证器是明智的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM