如何使用JQUERY调用局部视图

use*_*357 3 jquery partial-views getjson asp.net-mvc-4

我尝试使用ajax加载局部视图.但它给了我错误Internal char error.我调试代码然后调用操作以及调试局部视图但是,在部分视图的最后一行之后它给出了内部char错误.

以下是局部视图:

@model company.learn.data.Models.ProductViewModel
<div></div>
Run Code Online (Sandbox Code Playgroud)

行动代码如下:

  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);
     } 
Run Code Online (Sandbox Code Playgroud)

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>

}
Run Code Online (Sandbox Code Playgroud)

当我从局部视图中删除DIV元素并且只保留第一行(模型绑定)然后它没有给我错误但是,当我将任何元素添加到此视图中时它会给我错误.

请引导我做出奇怪的行为.

谢谢

Sar*_*nga 9

您正在使用$.getJSON并在控制器中返回一个PartialView.

更改$.getJSONajax呼叫并设置dataType: "html".

$.ajax({
    url: '/ProductManagement/LoadProducts',
    dataType: "html",
    success: function (data) {
        //$('#ProductsDiv').html(data);
    }
});
Run Code Online (Sandbox Code Playgroud)