I have this jquery code that uses two dropdown boxes and a controller action to get the values for the next dropdown. the location dropdown gets the id for the contacts dropdown:
public ActionResult GetBusinessContacts(int id)
{
var contacts = _repository.GetBusinessContacts(id);
//return Json(new SelectList(contacts.ToList()), JsonRequestBehavior.AllowGet);
var theJson = Json(new SelectList(contacts, "contactID", "lastName"));
var SelectListed = new SelectList(contacts, "contactID", "lastName");
return Json(new SelectList(contacts,"contactID","lastName"),JsonRequestBehavior.AllowGet);
}
No matter if I use result. or item. I always get undefined. My json result from my action looks fine when I debug. As far as I can tell I'm just not digging into my json result correctly.
Im so glad to hear that you solved this issue by yourself, and it is very appreciated for share the solution to us. Welcome to post any questions when you encounterd in the future programming.
Regards
Please mark the replies as answers if they help or unmark if not.
Feedback to us
viperassasin
Member
41 Points
53 Posts
Getting undefined from a jsonresult in cascade dropdown
Jun 25, 2012 09:12 PM|LINK
I have this jquery code that uses two dropdown boxes and a controller action to get the values for the next dropdown. the location dropdown gets the id for the contacts dropdown:
$("#LocationDropDown").change(function () { var locationID = $(this).val(); $.ajax({ url: "/Application/GetBusinessContacts/" + locationID, data: { id: locationID }, type: "POST", success: function (result) { $.each(result, function (index, item) { alert("Success:"); alert(result); alert(result.toString()); $("#PrimaryContactDropDown").get(0).options.length = 0; $("#PrimaryContactDropDown").get(0).options[0] = new Option("Select Contact", ""); $("#PrimaryContactDropDown").get(0).options[$("#PrimaryContactDropDown").get(0).options.length] = new Option(item.contactID, item.lastName); // $("#PrimaryContactDropDown").append("'<option value=" + result.contactID + ">" + result.lastName + "</option>'"); // $("#PrimaryContactDropDown").append($("<option></option>").val(this['contactID']).html(this['last_name'])); ; }); }, error: function (req, status, error) { alert("You failed"); }, datatype:'json' }); });Here is my action that i'm using:
public ActionResult GetBusinessContacts(int id) { var contacts = _repository.GetBusinessContacts(id); //return Json(new SelectList(contacts.ToList()), JsonRequestBehavior.AllowGet); var theJson = Json(new SelectList(contacts, "contactID", "lastName")); var SelectListed = new SelectList(contacts, "contactID", "lastName"); return Json(new SelectList(contacts,"contactID","lastName"),JsonRequestBehavior.AllowGet); }No matter if I use result. or item. I always get undefined. My json result from my action looks fine when I debug. As far as I can tell I'm just not digging into my json result correctly.
viperassasin
Member
41 Points
53 Posts
Re: Getting undefined from a jsonresult in cascade dropdown
Jun 25, 2012 09:51 PM|LINK
A selectlist has a properties of .Text and .Value so it needed to be item.Text and item.Value.
Young Yang -...
All-Star
21741 Points
1825 Posts
Microsoft
Re: Getting undefined from a jsonresult in cascade dropdown
Jun 28, 2012 03:32 AM|LINK
Hi
Im so glad to hear that you solved this issue by yourself, and it is very appreciated for share the solution to us. Welcome to post any questions when you encounterd in the future programming.
Regards
Feedback to us
Develop and promote your apps in Windows Store