communication between view and controller using ajax request

there are multiple ways of send a request to controller and returning data to populate dropdown or grid etc

1. send ajax request > return string array > parse to json > iterate through result



SERVER SIDE / CONTROLLER 
 
public string Get(string argSelectedValue)
{
     List resultList = new List();
     SelectListItem selListItem = new SelectListItem() { Value = "null", Text = "Select One" };
     resultList.Add(selListItem);

     JavaScriptSerializer js = new JavaScriptSerializer();
     return js.Serialize(new SelectList(resultList, "Value", "Text"));
}

 
CLIENT SIDE / AJAX REQUEST 
$.ajax({
      url: "/Lookup/Get",
      type: 'POST',
      data: { argSelectedValue: FromType },
      success: function (response) {
        var listItems = "";
        var json = $.parseJSON(response);

        for (var i = 0; i < response.length; i++) {
          listItems += "";
        }
        $("#dropdown_slash_grid").html(listItems);
      },
      error: function (xhr) {
        alert("Unable to perform operation");
      }
      });





2. send ajax request > return json array > iterate through result

 
SERVER SIDE / CONTROLLER 
 
public JsonResult Get(string argSelectedValue)
{
     List resultList = new List();
     SelectListItem selListItem = new SelectListItem() { Value = "null", Text = "Select One" };
     resultList.Add(selListItem);

     return Json(resultList, JsonRequestBehavior.AllowGet); 
}

public JsonResult Get(string argSelectedValue)
{
     List resultList = new List();
     SelectListItem selListItem = new SelectListItem() { Value = "null", Text = "Select One" };
     resultList.Add(selListItem);

     return Json(resultList.Select(x => new { Text = x.Text, Value = x.Value}), JsonRequestBehavior.AllowGet);
}
 
CLIENT SIDE / AJAX REQUEST 
$.ajax({
      url: "/Lookup/Get",
      type: 'POST',
      data: { argSelectedValue: FromType },
      success: function (response) {
        var listItems = "";

        for (var i = 0; i < response.length; i++) {
          listItems += "";
        }
        $("#dropdown_slash_grid").html(listItems);
      },
      error: function (xhr) {
        alert("Unable to perform operation");
      }
      });

Comments

Popular posts from this blog

storing byte array as column value in datatable using c#

post form data and bind with model : using jquery ajax in mvc

Htc sensation XL mobile not working with windows 7 or above, along with Htc sync software