views:

2783

answers:

16

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.

+1  A: 

My Way:

I am following the example shown in the Account controller that shipped with the Preview 4 release, where field is check in the controller and if an error is encountered then an array of string errors is aggregated for later on show in the same view that started the request.

I also have been following the thoughts of Stephen Walther on MVC and I think this post is great to repopulate the fields with the data submitted for it to be changed before submitting it again.

What do you guys use?

samiq
+5  A: 

I like to use the LiveValidation library within my ASP.NET MVC apps. With it, I was able to setup a way to to do validation on the client and on the server. That way the user will know something is wrong before even submitting the form and then use the server side functionality for the other outlying cases.

I started out with this post which does a very good job on how to setup a similar setup:

Model-based Client-side Validation for ASP.NET MVC

Dale Ragan
+1  A: 

Have you looked at the controls provided with the AjaxToolKit? I have used the MaskedEditExtender and the ValidatorCalloutExtender and have been very pleased with the results.

@Dale - thanks for correcting me on that one! Wasn't aware of that (just started using the toolkit last week). Please disregard my suggestion :)

Chuck
+6  A: 

@Chuck - You can't use the AjaxToolKit controls in ASP.NET MVC yet. They only work in the ASP.NET page postback model.

Update:

As of Aug 20, 2008, you can use the Script Files Only of the Ajax Control Toolkit inside of the ASP.NET MVC Framework. Here is a link to the release.

Also, here is a tutorial on using them to create a popup calendar by Stephen Walther.

I just want to make sure everybody is given the right information, if they read this thread.

Dale Ragan
A: 

Dale Ragan wrote:

You can't use the AjaxToolKit controls in ASP.NET MVC

How is AJAX handled on Stack Overflow? Does JQuery do it? In his Coding Horror post Secrets of the JavaScript Ninjas Jeff wrote about using JQuery while writing Stack Overflow.

Surely they didn't code it by hand. I did that once with all the XMLHttpRequest JavaScript when the term "AJAX" was popularized around 2005. It was a nightmare.

Zack Peterson
+1  A: 

@Zack: Yeap, I have read that post also and based off that post, SO uses jQuery as their javascript library. I can't speak for the SO team, but my educated guess is that more than likely they are using the ajax features inside of jquery to handle their ajax requests. It actually isn't that bad coding by hand if you use a javascript library like jQuery, Prototype, and MooTools. Then you use the newly JsonResult ActionResult return value for your actions and make ajax requests against them. These libraries encapsulate the XmlHttpRequest objects for you and make it easy to make ajax calls and browser safe.

Dale Ragan
A: 

@Chuck - No worries. That is what SO is for. You learn something and the person asking the question learns something.

Dale Ragan
+1  A: 

You can use the .net validation framework on codeplex if you want both client/server side validation. It generates code for the jQuery validate plugin based off rules defined in the model. Additionally, if you use the framework's "advanced" functionality you can manually control the jQuery validate plugin's settings. Checkout the mvc quickstart.

TheDeeno
+1  A: 

How is AJAX handled on Stack Overflow? Does JQuery do it?

  • Right click on the page
  • Look for 'View Page Source' in the pop-up menu
  • Click it

:-)

Orion Edwards
+21  A: 

Take a look at the JQuery Validation plugin this plugin is amazing,it's clean to implement and has all the features you could ever need, including remote validation via AJAX.

Also a sample MVC controller method can be found here which basically uses the JsonResult action type like:

public JsonResult CheckUserName(string username)
{
    return Json(CheckValidUsername(username));
}
Daniel Pollard
and now (with asp.net mvc2 preview 2+) jquery validation is integrated with model validation!
Victor Rodrigues
+4  A: 

There are some new validation features in Preview 5.

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

Matt Hinze
+1  A: 

Use a hybrid of client side validation and server side validation.

For client side validation, the approach described in the answer by Daniel Pollard seems sound. Client side validation is not mandatory but will provide the user with a nicer and much more responsive experience.

Server side validation, on the other hand, should be mandatory: never trust input from the client. I would definately look into the capabilities provided by the ASP.NET MVC framework in Preview 5 (as described in the answer by Matt Hinze),

Kasper
+1  A: 

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.

Read the post over here

eibrahim
+5  A: 

IMO using xVal with jQuery and DataAnnotationsModelBinder is the best combination.

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

  • 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.

I wrote a blog article on this describing all the details.

Adrian Grigore
I don't know how you didn't get more ups. This is AWESOME, thank you!
jcm
A: 

If you want to stick with the out-of-the-box functionality but need to extend it take a look at my answer here to a question about IDataError.

Basically I use data annotations and a modelbinder for more complicated logic. I've found it to be a pretty clean way and lets me stay within the bounds of what MS is providing.

I explain how to use a modelbinder to add 'class-level' checking of your model without having to use IDataError - which as you have seen here can be quite clumsy. It still lets you use [Required] attributes or any other custom validation attributes you have, but lets you add or remove individual model errors. For more on how to use data annotations I highly recommend this post from Scott Gu.

Simon_Weaver
A: 

One of the ways to implement validation in ASP.NET MVC is to use Data Annotations along with client side validation. This article shows how we can do that for entity framework generated model classes.

Bikal Gurung