【发布时间】:2014-02-17 09:54:28
【问题描述】:
我正在使用 ASP.NET MVC 4。单击按钮时,我想调用控制器方法的 ajax 回调并将所需的数据作为 Json 数据返回。我可以使用以下代码来做到这一点:
<script>
$(document).ready(function () {
var ajax_url = '@Url.Action("GetNewItems")';
$("#submit").click(function () {
$.ajax({
url: ajax_url,
type: "POST",
datatype: "json",
success: function (data) {
debugger
}
});
});
});
[HttpPost]
public JsonResult GetNewItems()
{
List<Item> items = new List<Item>();
items.Add(new Item() { Id = 3, Name = "c" });
items.Add(new Item() { Id = 4, Name = "d" });
return Json(items);
}
在success函数中,Item集合被正确返回。在这个函数中,我想调用 Html.Partial 并将结果作为 Partial 视图的模型传递。这可能吗?
我在其他线程中搜索过,但大多数都建议从 Controller 方法返回 Partial View 并使用 html(data) 来呈现它。我宁愿不从 Controller 返回 Partial 视图,因为大小会有很大差异,而不是只返回数据并在客户端手动呈现。
那么,是否可以在成功函数中将结果传递给部分视图,或者我必须手动渲染其中的元素?
感谢任何帮助。谢谢!
【问题讨论】:
标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4