Reputation: 2642
I have an action of the controller that return the result as the JSON result.(I test it ready, It works well with this action).
public JsonResult GetProductsByDepList(int id)
{
JsonResult jr = new JsonResult();
var _product = from a in DataContext.GetProductsByDep(id)
select new { ID = a.ID, ProName = a.Name};
jr.Data = _product.ToList();
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return jr;
}
And this is what I loop it in my view :
$(document).ready(function () {
var urlProduct = '<%: Url.Content("~/") %>' + "Products/GetProductsByDepList";
$.getJSON(urlProduct, function (dataPro) {
alert(123);
});
});
I just test it with alert, but it doesn't alert anything when I load my page. Could anyone tell me how to use Action of controller in jquery??
Thanks and welcome all your answers.
Upvotes: 0
Views: 542
Reputation: 2339
you can use with $.ajax in jquery if your json format like this
{"rows":[{"id":"1","username":"foo"},{"id":"2","username":"bar"}]}
$.ajax({
url: urlProduct,
type:"GET",
success:function (data) {
$.each(data.rows,function(i,rows){
alert(rows.username);//will be show username foo and bar
});
}
});
this is the reference http://api.jquery.com/jQuery.ajax/
and this is reference to json http://www.json.org/
this is library can use for C# http://sourceforge.net/projects/csjson/
Upvotes: 1