I cannot get this to work no matter how much searching and different variations I keep trying:
I am trying to dynamically populate a select list from my jquery:
var url = '@Url.Action("GetCounties", "Account")' + '/' + $("#State").val();
$.get(url, function (data) {
$('#county').empty();
$.each(result, function (index, val) {
$('#county')
.append($("<option></option>")
.attr("value", val.Text)
.text(val.Text));
});
});
Controller Action using Entity Framework:
public JsonResult GetCounties(string id)
{
return Json(GetCountySelectList(id), JsonRequestBehavior.AllowGet);
}
private SelectList GetCountySelectList(string id)
{
var counties = db.ZipCodeDataBase.Where(x => x.State.Contains(id)).OrderBy(x => x.County).Select(x => x.County).Distinct().ToList();
SelectList list = new SelectList(counties);
return list;
}
Every variation of the generated select list will not return the data as it is required by the jquery.
Any assistance would be GREATLY appreciated!
You are calling your return variable
dataand then referencing it asresultin the$.each. Try fixing that first.