我尝试使用ajax加载部分 View 。但它给了我错误内部字符错误
。
我调试代码,然后它调用操作,并且还能够调试部分 View ,但是在部分 View 的最后一行之后,它给了我内部字符错误。
下面是部分 View :
@model company.learn.data.Models.ProductViewModel
<div></div>
Action 代码如下:
public ActionResult LoadProducts()
{
RepositoryProductManager m = new Repository.ProductManager();
var p = m.RetrieveAllProducts();
var l = p.Select(o => new ProductViewModel() { Cost = o.Cost, Description = o.Description, Id = o.Id, ProductName = o.ProductName, ProductTypeName = o.ProductType.Name, ProductTypeId = o.ProductTypeId }).ToList().FirstOrDefault();
return PartialView("_LoadProducts",l);
}
jquery ajax 调用:
@section scripts
{
<script>
$.getJSON('@Url.Action("LoadProducts","ProductManagement")', null, function (data) {
alert('f');
//$('#ProductsDiv').html(data);
alert('f');
}
).error(function (e1, e2, e3) { alert(e3); }); //every time goes to alert this error.
</script>
}
当我从部分 View 中删除 DIV 元素并仅保留第一行(模型绑定(bind))时 然后它没有给我错误,但是,每当我向此 View 添加任何元素时,它都会给我错误。
请指导我的奇怪行为。
谢谢
最佳答案
您正在使用 $.getJSON
并在 Controller 中返回 PartialView
。
将 $.getJSON
更改为 ajax
调用并设置 dataType: "html"
。
$.ajax({
url: '/ProductManagement/LoadProducts',
dataType: "html",
success: function (data) {
//$('#ProductsDiv').html(data);
}
});
关于jquery - 如何使用JQUERY调用部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25102082/