-2

I am new to MVC.

I am having one form, let's call this as Registration Form. In that I have 10 textboxes(id,name,address..etc) Once user enters the Id -I have to check in DB it's already available or not and then i need to display status.

Is it possible in MVC?? without Clicking on submit button ??

Thanks in Advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 4
    It's possible. Check in the jQuery API for `keypress` and `$.ajax`. You'll also need an endpoint in your controller to check for duplicates already in the database. – Rory McCrossan Apr 28 '14 at 13:31
  • 1
    are you aware that "MVC" is a **language independent design pattern** and not a name for a framework – tereško Apr 28 '14 at 13:34

1 Answers1

5

Yes it is in fact not hard to achieve this. You can use RemoteAttribute on the property of your model that you want to be validated asynchronously on the server, in your case it is id.

http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v=vs.118).aspx

// model
public class MyModel
{
   [Remote("ValidateId", "MyController")]
   public string Id { get; set; }
}


// controller
public class MyController
{
  public ActionResult ValidateId(string id)
  {
    // action will be invoked when you change value in the browser.
    // you have to return a string that contains an error if the id fails validation.
    // or true if the id is valid.

    // this is in case id is valid
    // return Json(true, JsonRequestBehavior.AllowGet);

    // this in case id is not vlaid.
    // return Json("id is not valid", JsonRequestBehavior.AllowGet);

  }
}

Take a look at this also:

http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx

milagvoniduak
  • 3,214
  • 1
  • 18
  • 18