ASP.NET MVC Server Side Remote Validation

In this quick post, I will show you a way of implementing ASP.NET MVC Server Side Remote Validation just like ASP.NET MVC Remote Validation
2011-11-06 19:10
Tugberk Ugurlu


go-to-server-and-come-back

For nearly 15 days, I have been poking around on the internet, books, etc. for anything related to ASP.NET MVC validation. I have to say that validation features in ASP.NET MVC framework is really outstanding.

I have blogged about Remote Validation twice so far: Check Instantly If Username Exists - ASP.NET MVC Remote Validation and ASP.NET MVC Remote Validation For Multiple Fields With AdditionalFields Property.

Remote validation is one of the useful stuff which is baked into MVC framework. It is easy to implement this feature manually with JQuery but it is kind of nice to have it out of the box. So, thanks a lot ASP.NET MVC team.

One thing which ASP.NET MVC Remove Validation is missing is no support for server side validation of the chosen property. There is probably a good reason not to support it out of the box though.

So, I decided to build one for myself. When I implemented it, I thought it worked fine and I should blog about it. So, I am writing this short blog post. But, on the other hand I am also sure that no one should use this on production at least for nowSmile I don’t know why but it feels like it is not stable. Maybe you can use it if you see a positive comment from an MSFT person on this post.

Here how it works:

First of all, we need to use Nuget here to bring down a very small package called TugberkUg.MVC which will have the necessary stuff for server side remote validation to work.

image

After that, here how we can use it:

  [Required]
  [Display(Name = "User name")]
  [Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]
  [ServerSideRemote("Account", "doesUserNameExistGet")]
  public string UserName { get; set; }

You do not need to use ASP.NET MVC Remote validation along with ServerSideRemote validation but in a real world scenario, we probably would use both.

As you can see, there is not much to specify but of course, you get the options properties of ValidationAttribute class along with it. there is not much to specify there because I have built it for like 30 minutes. So, spare me for that.

When we look at our controller action which holds the validation logic, we will see that it is not much different that Remote validation’s:

public JsonResult doesUserNameExistGet(string term) {

    var user = Membership.GetUser(term);

    return Json(user == null, JsonRequestBehavior.AllowGet);
}

When I fire up the register page, I am getting the following screen :

image

I have excluded the JavaScript files for client side validation because we do not want to see client validation working. I already have a user registered whose user name is User1. When I completed the fields, I am getting no warning:

image

Let’s push the Register button :

image

Bingo! We got it working. Let’s try a legitimate one:

image

That worked as well.

I also put the sample project on GitHub so you can get the working sample code if you want :

https://github.com/tugberkugurlu/MvcRemoteValidationSample

Also, you can find the whole TugberkUg.MVC package’s code on BitBucket:

https://bitbucket.org/tugberk/tugberkug.mvc/src



Comments

Iffi
by Iffi on Tuesday, Oct 01 2013 12:12:17 +03:00

Glad to read your post :). It is very informative!

André Silva
by André Silva on Thursday, Feb 06 2014 12:14:06 +00:00
hello. I am having problem when i try to use [ ServerSideRemote] data annotation with MVC code first. When app starts in global.asax, the Model Bind displays the error on "db.Database.Initialize(true);" Model class property: [ServerSideRemote("User", "UniqueLoginGet", ErrorMessage = "xxxx")] public string Login { get; set; } Error: An unexpected exception was thrown during validation of 'Login' when invoking TugberkUg.MVC.Validation.ServerSideRemoteAttribute.IsValid. See the inner exception for details. Can you help me with this model bind?

New Comment