I think your problem is with the return type. Change ActionResult with JsonResult. And good to use contentType: "application/json; charset=utf-8". You can refer the following working posts-
I have another controller that has the same code (actionresult, return Json). That one works fine. It is part of an autocomplete script which is working fine:
Controller:
public ActionResult AutoCompleteCompany(string query)
{
var companyNames = (from c in db.t_company where c.CompanyName.Contains(query) select c.CompanyName).Distinct().Take(5).ToArray();
return Json(companyNames);
}
robin.curtis...
Member
231 Points
87 Posts
Jquery, Ajax, JSON MVC3
May 24, 2011 05:23 AM|LINK
Hi Guys,
I have a jquery script that I am trying to get to do cascading dropdowns with.
I have a controller that returns a JSON object with an ID and a Value. Controller looks like this:
public ActionResult ProvinceFilter(int cID) { var provinces = (from p in db.t_province where p.Country == cID select p); return Json(provinces); }I then call the change of dropdownlist function and the alert works, but i never get a success on the JSON call:
$("#Country").change(function () { var cSelected = $("#Country option:selected").val(); alert(cSelected); $.ajax({ url: "/Funclib/ProvinceFilter", type: "POST", dataType: "json", data: ({ cID: cSelected }), success: function (provinces) { $("#Province").find('option').remove(); alert("inside"); } }); alert(cSelected); });Any idea what I am doing wrong? I have used the alert("inside") to alert if there is a success but it never alerts.
raduenuca
All-Star
24675 Points
4250 Posts
Re: Jquery, Ajax, JSON MVC3
May 24, 2011 08:53 AM|LINK
See if this can help:
http://forums.asp.net/p/1640286/4243267.aspx/1?Re+Drop+Down+List+as+Filter+on+List+View
Radu Enuca | Blog
asteranup
All-Star
30184 Points
4906 Posts
Re: Jquery, Ajax, JSON MVC3
May 24, 2011 09:49 AM|LINK
Hi,
I think your problem is with the return type. Change ActionResult with JsonResult. And good to use contentType: "application/json; charset=utf-8". You can refer the following working posts-
http://forums.asp.net/p/1655438/4311291.aspx/1?Re+Correct+way+to+fire+controller+method+with+Jquery
http://forums.asp.net/p/1650403/4292279.aspx#4292279
http://forums.asp.net/t/1627118.aspx
http://forums.asp.net/p/1658781/4324955.aspx
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
robin.curtis...
Member
231 Points
87 Posts
Re: Jquery, Ajax, JSON MVC3
May 24, 2011 11:11 AM|LINK
I have another controller that has the same code (actionresult, return Json). That one works fine. It is part of an autocomplete script which is working fine:
Controller:
public ActionResult AutoCompleteCompany(string query) { var companyNames = (from c in db.t_company where c.CompanyName.Contains(query) select c.CompanyName).Distinct().Take(5).ToArray(); return Json(companyNames); }Java Script:
$(document).ready(function () { $("#SearchString").autocomplete({ source: function (request, response) { $.ajax({ url: '/Funclib/AutoCompleteCompany', type: "POST", dataType: "json", data: { query: request.term }, success: function (data) { response($.map(data, function (item) { return { label: item, value: item }; })) } }) }, minLength: 2 }); });bruce (sqlwo...
All-Star
37614 Points
5573 Posts
Re: Jquery, Ajax, JSON MVC3
May 24, 2011 03:12 PM|LINK
you are trying to json serialize a iqueryable. try:
return Json(from p in db.t_province where p.Country == cID select p).ToList();ymetelkin
Member
8 Points
4 Posts
Re: Jquery, Ajax, JSON MVC3
May 24, 2011 07:22 PM|LINK
Your Ajax request is of type "POST" but your controller method is of default type "GET".
Try adding [HttpPost] attribute to your controller method.
robin.curtis...
Member
231 Points
87 Posts
Re: Jquery, Ajax, JSON MVC3
May 25, 2011 05:15 AM|LINK
Tried both of the previous suggestions, still no luck.
robin.curtis...
Member
231 Points
87 Posts
Re: Jquery, Ajax, JSON MVC3
May 25, 2011 05:20 AM|LINK
Needed to add:
JsonRequestBehavior.AllowGet
to my controller code.