我正在尝试在 MVC 3 中使用部分 View 填充 div。我的 Controller 如下所示:
[HttpPost]
public ActionResult GetCustomerList(string searchString)
{
var custService = new CustomerViewModels();
var custListVM = custService.GetSearchList(searchString);
return PartialView("GetCustomersList", custListVM);
}
我的 jQuery 调用如下所示:
function getCustomerList(searchCriteria) {
$.ajax({
url: 'Home/GetCustomerList',
type: 'POST',
async: false,
data: { searchString: searchCriteria },
success: function (result) {
$("#customerTabBody").html(result);
}
});
};
效果很好。我开始想知道是否可以使用 jQuery load 方法以更少的代码完成同样的事情,并编写了以下代码:
function getCustomerList(searchCriteria) {
$("#customerTabBody").load('Home/GetCustomerList', { searchString: searchCriteria });
};
它返回一个错误,指出找不到资源。我尝试过使用@Url.Action,但它编码的 Controller 路径与我硬编码的相同。在 Firebug 中,我可以看到发布到的 URL 是相同的,并且两个实例中的 searchString 参数的格式相同。
有什么区别 - 为什么加载不起作用?
谢谢
最佳答案
Jquery 文档 load说...
The POST method is used if data is provided as an object; otherwise, GET is assumed.
由于您将数据作为对象传递,因此根据文档,它必须是后调用。此外,您通过 $.ajax 从 Controller 获取所需的数据,因此 Controller 操作方法似乎没问题。使用load发送ajax请求时可能会出现一些错误。你可以在 Firebug 中检查它。
关于jQuery AJAX 可以返回 mvc 3 部分 View ,但 jQuery load 不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811625/