Aleena Saviour

Aleena Saviour

  • NA
  • 519
  • 87.7k

bind json result from controller action to view in ajax

Jul 10 2017 2:27 AM
Here is my code:
action:
[HttpGet]
public JsonResult GetProducts(int catId)
{
List<ProdDisplay> lstprods = new List<ProdDisplay>();
try
{
con.Open();
if(con.State == ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand("Prod_Select", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PKCatId", catId);
cmd.Parameters.AddWithValue("@PKSubCatId", 0);
cmd.Parameters.AddWithValue("@PKProdId", 0);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
lstprods.Add(new ProdDisplay
{
PKProductId = Convert.ToInt32(dr[0]),
ProductName = dr[1].ToString(),
ProdDescp = dr[2].ToString(),
Price = Convert.ToDouble(dr[3].ToString())
,
Discount = Convert.ToDouble(dr[4].ToString()),
shipCharge = Convert.ToDouble(dr[5].ToString()),
Category = dr[7].ToString(),
SubCategory = dr[9].ToString(),
Path1 = dr[10].ToString(),
Path2 = dr[11].ToString()
});
}
con.Close();
}
}
catch (Exception ex)
{
con.Close();
}
return Json(lstprods, JsonRequestBehavior.AllowGet);
}
 
ajax call:
 
function selProd(id)
{
alert('select products=' + id);
$.ajax({
type: "GET",
dataType: "json",
url: '@Url.Action("GetProducts", "Items")',
data: { 'catId': id },
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
handledata(data);
},
error: function(xhr, status, error) {
alert("error"+error);
}
});
}
 
I want to bind result from action to view in ajax success..Please help me with suitable code?
 
 
 

Answers (1)