在 MVC 中通过 Ajax 加载部分视图后显示部分视图
本文关键字:视图 显示部 加载部 Ajax MVC | 更新日期: 2023-09-27 18:33:36
我想在用户在页面上时在后台加载数据。加载数据后,我想使用部分视图显示它。所以我写了这个:
var serviceURL = '/Vm/GetInformation';
$.ajax({
type: "POST",
url: serviceURL,
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { "id": id },
//success: resetPartial()
});
在我的控制器中:
public ActionResult GetInformation()
{
myCode...
return PartialView("_List", Costumer);
}
那么在我的主视图中显示该_List部分视图的最后一步是什么,该视图现在充满了有关客户的数据(我的 ajax 函数所在的位置(
谢谢
您需要做的就是替换旧内容:
success: function(d) {
//d should be a string that's the HTML markup
$("#someparentelementthatsurroundsinnercontent").html(d);
}
或者,如果要追加到列表末尾,可以使用任何方法追加内容。 JQuery提供了很多选项来做到这一点。
控制器操作不需要任何参数。因此,您可以从 ajax 调用中删除以下行。
data: param = "",
data: { "id": id },
如果您的控制器操作中确实有任何参数 ID,请像
data: { id:id }
在 html 中有一个div 来保存分部视图的内容。在成功时,使用如下:
success : function(result) {
$("#partialViewHolder").html(result);
}