jquery - 如何使用JQUERY调用部分 View

标签 jquery asp.net-mvc-4 partial-views getjson

我尝试使用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/

相关文章:

jquery - 关于jquery中的创建函数

jquery - 由于字符串排序,对整数的可容纳列进行排序会呈现意外结果

asp.net-mvc - 测量调用 ASP.NET MVC Controller 操作的时间

c# - 使用 Aspx 页面作为 razor View 的局部 View

asp.net-mvc - MVC中局部 View 和用户控件的区别

javascript - 在指定时间后多次调用 Javascript 中的函数

javascript - Laravel 5.5 jQuery 3.3.1 ajax 不是函数

c# - 简单发布到 Web Api

javascript - 有条件地在 Angular js 中包含头文件

javascript - 更改 HTML 标签,包括同一标签的子标签