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
6 November 2011
3 minutes read

Related Posts

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