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
2. send ajax request > return json array > iterate through result
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) { ListresultList = new List } public JsonResult Get(string argSelectedValue) { List(); SelectListItem selListItem = new SelectListItem() { Value = "null", Text = "Select One" }; resultList.Add(selListItem); return Json(resultList, JsonRequestBehavior.AllowGet); 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
Post a Comment