Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 1.68 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to call Json Method on textbox change event using jquery in asp.net mvc?
  2. public JsonResult GetUserName(string emailId)
  3. {
  4.     string UserName = "";
  5.     var query = (from u in DbContext.Users
  6.                  where u.User_Email.ToLower() == emailId.ToLower()
  7.                  select u).SingleOrDefault();
  8.     if(query!=null)
  9.     {
  10.         UserName = query.User_FirstName +", "+query.User_LastName;
  11.     }
  12.     return Json(UserName, JsonRequestBehavior.AllowGet);
  13. }
  14.        
  15. Email:&nbsp;<input type="text" id="txtEmail" /><br /><br />
  16. Name:&nbsp;<input type="text" id="txtName" />
  17.        
  18. <script type="text/javascript">
  19.         $(document).ready(function () {
  20.             //$("#txtEmail").first().focus();
  21.  
  22.             $("#txtEmail").change(function () {
  23.                 var emailId = $(this).val();
  24.                 //$("#txtName").val(emailId);
  25.                 $.ajax({
  26.                     url: 'GetUserName',
  27.                     type: 'POST',
  28.                     data: JSON.stringify({ emailId: emailId }),
  29.                     dataType: 'json',
  30.                     contentType: 'application/json',
  31.                     success: function (data) {
  32.                         if (data != "" || data != null) {
  33.                             $("#txtName").val(data);
  34.                         }
  35.                         else {
  36.                             alert("Our system does not recognize this email. Please try again.");
  37.                             $("#txtEmail").focus();
  38.                         }
  39.                     }
  40.                 });
  41.             });
  42.         });
  43.     </script>
  44.        
  45. $.ajax({
  46.    url: '@(Url.Action("GetUserName"))',
  47.    type: 'POST',
  48.        
  49. $.ajax({
  50.     url: '<%: Url.Action("GetUserName") %>',
  51.     type: 'POST'