views:

466

answers:

6

Hi,

Using .net MVC and I am doing some server side validation in my Action that handles the form post.

What is a good technique to pass the errors back to the view?

I am thinking of creating an error collection, then adding that collection to my ViewData and then somehow weave some javascript (using jQuery) to display the error.

It would be nice it jQuery had some automagic way of display an error since this is a common pattern.

What do you do?

+1  A: 

ViewData.ModelState is designed to pass state information (errors) from the controller to the view.

Mehrdad Afshari
+4  A: 

You want to add the errors to the ModelState as @Mehrdad indicates.

...
catch (ArgumentOutOfRangeException e)
{
    ModelState.AddModelError( e.ParamName, e.Message );

    result = View( "New" );
}

And include the ValidationSummary in your View

<%= Html.ValidationSummary() %>
tvanfosson
To expand on this, it also adds a validation-error class to inputs created using HtmlHelpers, and you can display field unique messages anywhere in your view using HtmlHelpers that use the same ModelState. A framework is in development for unifying server/client validation: google xval.
eyston
A: 

Yes it's good way but anyway you can put error message to your own ViewData["key"] = "Error on page...bla...bla...bla..."

<% if (!string.IsNullOrEmpty(ViewData["key"]+"")) { %>

<div>
Yor customized error template
</div

<% } %>
omoto
That's doing validation in the view, which goes against what MVC is all about.
David Brown
A: 

Not quite sure if it's what your looking for but there is a very easy to use form validation plugin for jquery at http://bassistance.de/jquery-plugins/jquery-plugin-validation/. It automagically displays error messages in red.

Ofcourse you still have to do server side validation, and pass back the errors. tvanfosson showed you how in his answer.

Morph
A: 

I use the builtin ModelState object hold my validation errors. Validation is done either in binding or by hand supported by manually adding the errors like this:

ModelState.AddModelError("LastName","Last name can't be Doe").

To support the ajax form post scenario, I have made an extension method to the ModelStateDictionary, GetErrors(), that returns a light ModelStateErrorsDTO object (a flattened version of the modelstate's validation errors suitable for json serialization).

When a form post is an ajax request I then return a json serialized ModelStateErrorsDTO.

On the jquery side I have written a helper function that places the validation errors next to the relevant inputfields using the default mvc css classes, i.e. input-validation-error.

This way you will be able to make unobtrusive ajaxforms with validation messages.

Hope this helps.

Christian Dalager
Do you have an example of this?
Funky81
I would love to see an example too. I've been trying to figure this out for a while and never thought to do pass ModelStateErrors as a JSON object
woopstash
A: 

What about passing back a success message. Should this go in AddModelError? I want the message to go in validationsummary

It's not an error, so to keep things clean, I wouldn't use AddModelError for such a thing. What I did was create a "ViewMessages" system that allows me to associate a message with a ViewMessageType.(Success|Error|Info|Warning) enum and use a custom HtmlHelper extension to display them.
David Brown