How to use Ajax in Asp.Net MVC

This blog includes a step by step tutorial to learn ajax and get effective way response in success with the help of ASP.Net MVC.

This blog includes a step by step tutorial to learn ajax and get effective way response on success with the help of ASP.Net MVC.
  1. Create a class in project Blresult.cs

    This class is generic List<T> class so you can assign any type of data and get a response in an effective way.
    1. using System;    
    2. using System.Collections.Generic;    
    3. using System.Linq;    
    4. using System.Web;    
    5.     
    6. namespace TestAPP.Helper    
    7. {    
    8.     /// <summary>    
    9.     /// Base Class for returning result used in Pubmed Services as well Windows application    
    10.     /// </summary>    
    11.     [Serializable]    
    12.     public class BLResult    
    13.     {    
    14.         public BLResult()    
    15.         {    
    16.             //    
    17.             // TODO: Add constructor logic here    
    18.             //    
    19.         }    
    20.         private string _ResultMessage = string.Empty;    
    21.         private bool _Success = false;    
    22.         public bool Success    
    23.         {    
    24.             get { return _Success; }    
    25.             set { _Success = value; }    
    26.         }    
    27.         public string ResultMessage    
    28.         {    
    29.             get { return _ResultMessage; }    
    30.             set { _ResultMessage = value; }    
    31.         }    
    32.     }    
    33.     /// <summary>    
    34.     /// Generic class for returning result used in Pubmed Services as well Windows application    
    35.     /// </summary>    
    36.     /// <typeparam name="T"></typeparam>    
    37.     [Serializable]    
    38.     public class BLResult<T> : BLResult    
    39.     {    
    40.         protected T _GenericValue = default(T);    
    41.     
    42.         public BLResult()    
    43.         { }    
    44.     
    45.         public T Value    
    46.         {    
    47.             get { return _GenericValue; }    
    48.             set { _GenericValue = value; }    
    49.         }    
    50.     }    
    51. }  
  2. Create method on HomeController.cs

    Currently, I have declared integer value in Blresult. We can also use a class instead of integer value, but type of data added in Blresult you must set that type of data in response.
    1. public JsonResult GetTestData(int Id) {  
    2.  try {  
    3.   BLResult < int > blresult = new BLResult < int > ();  
    4.   if (Id != 0) {  
    5.    blresult.Success = true;  
    6.    blresult.ResultMessage = "Data get successfully";  
    7.    blresult.Value = 25;  
    8.   } else {  
    9.    blresult.Success = false;  
    10.    blresult.ResultMessage = "Data is not found";  
    11.    blresult.Value = 0;  
    12.   }  
    13.   return Json(blresult, JsonRequestBehavior.AllowGet);  
    14.   
    15.  } catch (Exception ex) {  
    16.   
    17.   throw;  
    18.  }  
    19. }
  3. Call action method using ajax and handle response in ajax success.

    This ajax example is given result in success and we can handle easily. So we can manage data easily on success response.
    1. $.ajax({    
    2.          type: "GET",    
    3.          url: "/Vendor/GetTestData/2",    
    4.          contentType: "application/json; charset=utf-8",        
    5.          success: function (result) {    
    6.              if (result.Success) {    
    7.                  $("#txtTest").val(result.Value)    
    8.                  alert(result.ResultMessage);    
    9.              }    
    10.              else {    
    11.                  console.log(result.ResultMessage)    
    12.   
    13.              }    
    14.   
    15.          },    
    16.          error: function (error) {    
    17.              console.log(error);    
    18.          }    
    19.      });
Thank you and happy coding.