[HttpPost] public JsonResult CheckIfUnique(string Username) { return Users.Any(name => name.ToUpper() == Username.ToUpper()) ? // any non-true value is considered an invalid input Json(Username + " is already taken. Try another user name") : Json(true); } IList<string> Users { get { return new string[] { // common names "Michael", "John", "Jacob" }; } }
On your model:
[Required] [Remote("CheckIfUnique","Home", HttpMethod="post")] public string Username { get; set; }
That will call the CheckIfUnique method of HomeController automagically while you are entering an input. Gone are the days for you to manually code a javascript that invokes the controller/method and check if your input has a duplicate. It's all in the Model Luke! May the force(validations) be with you!
No comments:
Post a Comment