<p>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.  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).</p>

[{'answer_id': 10311, 'author': 'Pete', 'author_id': 76, 'author_profile': 'https://Stackoverflow.com/users/76', 'pm_score': 2, 'selected': False, 'text': '<p>Have you taken a look at this?\n<a href="http://www.codeplex.com/MvcValidatorToolkit" rel="nofollow noreferrer">http://www.codeplex.com/MvcValidatorToolkit</a></p>\n\n<p>Quoted from the page</p>\n\n<blockquote>\n  <p>The Validator Toolkit provides a set\n  of validators for the new ASP.NET MVC\n  framework to validate HTML forms on\n  the client and server-side using\n  validation sets.</p>\n</blockquote>\n\n<p>I\'m afraid that someone more MVC-savvy than me would have to speak to where in the architecture you should put things.</p>\n'}, {'answer_id': 10364, 'author': 'Iain Holder', 'author_id': 1122, 'author_profile': 'https://Stackoverflow.com/users/1122', 'pm_score': 0, 'selected': False, 'text': '<p>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.  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.</p>\n\n<pre><code>Update(int id, string name, string foo)\n</code></pre>\n\n<p>You could do the validation within that method.  If all is ok, </p>\n\n<pre><code>return View("Item", yourObject)\n</code></pre>\n'}, {'answer_id': 10467, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 4, 'selected': False, 'text': '<p>Here\'s an overview of the flow in MVC:</p>\n\n<ol>\n<li>/new - render your "New" view containing a form for the user to fill out\n\n<ul>\n<li>User fills out form and it is posted to /create</li>\n<li>The post is routed to the Create action on your controller</li>\n<li>In your action method, update the model with the data that was posted.</li>\n<li>Your Model should validate itself.</li>\n<li>Your Controller should read if the model is valid.</li>\n<li>If the Model is valid, save it to your db.  Redirect to /show to render the show View for your object.</li>\n<li>If the Model is invalid, save the form values and error messages in the TempData, and redirect to the New action again.  Fill your form fields with the data from TempData and show the error message(s).</li>\n</ul></li>\n</ol>\n\n<p>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.</p>\n'}, {'answer_id': 22213, 'author': 'Artem Tikhomirov', 'author_id': 2313, 'author_profile': 'https://Stackoverflow.com/users/2313', 'pm_score': 0, 'selected': False, 'text': '<p>There is <a href="http://209.85.135.104/search?q=cache:EzaP2dgUM9cJ:hammett.castleproject.org/%3Fp%3D114+Castle.Components.Validator&amp;hl=ru&amp;ct=clnk&amp;cd=2&amp;gl=ru&amp;client=firefox-a" rel="nofollow noreferrer">Castle.Components.Validator</a> module in Castle project. 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.\nOf course it\'s wise to abstract validator away behind IValidationEngine interface. </p>\n'}, {'answer_id': 45655, 'author': 'Robert Dean', 'author_id': 3396, 'author_profile': 'https://Stackoverflow.com/users/3396', 'pm_score': 3, 'selected': True, 'text': '<p>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:</p>\n\n<p><a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx</a></p>\n'}, {'answer_id': 101966, 'author': 'Switters', 'author_id': 1860358, 'author_profile': 'https://Stackoverflow.com/users/1860358', 'pm_score': 2, 'selected': False, 'text': '<p>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.  When I was just playing around with it for myself I created a ModelBinder for the LinqToSql data class that I had generated.  You can check out this post to find out how to put together a basic ModelBinder:</p>\n\n<p><a href="http://blog.maartenballiauw.be/post/2008/08/29/Using-the-ASPNET-MVC-ModelBinder-attribute.aspx" rel="nofollow noreferrer">ASP.Net MVC Model Binder</a></p>\n\n<p>The in your action if you had created a "Product" ModelBinder you would just declare the action like so:</p>\n\n<p>public ActionResult New(Product prod)</p>\n\n<p>And the model binder will take care of assigning posted data to the objects properties as long as you\'ve built it right anyway.</p>\n\n<p>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:</p>\n\n<p>(ModelStateDictionary_name).AddModelError("form_element_id", "entered_value", "error_message");</p>\n\n<p>Then you can just throw a &lt;%= Html.ValidationSummary() %> in your view to display all your errors.</p>\n\n<p>For client-side validation I just used 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.</p>\n'}]
['https://Stackoverflow.com/questions/10300', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/203/']