I’m trying to return a list of json objects to the view, however it’s not liking that its an object, it will work if i return a string or a list of strings. But really I don’t need the whole object in the view, I really just need the name and the Id. How can i change this to return a list of the names and matching Id to the View and then insert/update all the names into a listbox? Any help is appreciated. 🙂
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetUserStories(int id)
{
var userstorylist = userRepository.Select().Where(x => x.EpicId.Equals(id)).ToList();
var userstorynames = userstorylist.Select(x => x.Name);
return Json(userstorynames, JsonRequestBehavior.AllowGet);
}
View:
$.getJSON("/Estimate/GetUserStories", $.param({ id: selectedid }, true), function (result) {
$(".UserStoryListBoxClass").html("");
for (var item in result) {
$(".UserStoryListBoxClass").append($("<option>" + result[item] + "</option>"));
}
});
If your
jsonresult is something like thisThe below code will work,
Working sample http://jsfiddle.net/CFdeW/14/
Never do the
appendmethod in the loop for adding elements to a drop down. Always store it into a variable and call the html method only once.