【发布时间】:2014-10-07 21:38:39
【问题描述】:
我需要从 mvc 控制器获取列表以使用 jquery ajax 进行查看。我怎样才能做到这一点。这是我的代码。它的警告错误消息。
在控制器中
public class FoodController : Controller
{
[System.Web.Mvc.HttpPost]
public IList<Food> getFoodDetails(int userId)
{
IList<Food> FoodList = new List<Food>();
FoodList = FoodService.getFoodDetails(userId);
return (FoodList);
}
}
在视图中
function GetFoodDetails() {
debugger;
$.ajax({
type: "POST",
url: "Food/getFoodDetails",
data: '{userId:"' + Id + '"}',
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
debugger;
alert(result)
},
error: function (response) {
debugger;
alert('eror');
}
});
}
【问题讨论】:
-
将方法返回类型更改为ActionResult,并将您的列表返回为
return Json(new { MyList = FodList}, JsonRequestBehavior.AllowGet); -
问题解决了.....???
标签: javascript jquery ajax asp.net-mvc jquery-ajaxq