如何使用 JQUERY 调用局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25102082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:03:08  来源:igfitidea点击:

How to call partial view using JQUERY

jqueryasp.net-mvc-4partial-viewsgetjson

提问by user3711357

I try to loadn partial view using ajax. But it gives me error as Internal char error. I debug the code then it call the action as well as also able to debug the the partial view but, after last line of partial view it gives me internal char error.

我尝试使用 ajax 加载部分视图。但它给了我错误为Internal char error. 我调试代码,然后它调用操作,也能够调试局部视图,但是,在局部视图的最后一行之后,它给了我内部字符错误。

below is the partial view:

以下是部分视图:

@model company.learn.data.Models.ProductViewModel
<div></div>

Action code is as follow:

动作代码如下:

  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 call:

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>

}

When i remove the DIV element from partial view and only keep first line (model binding) then it did not give me error but, whenevery i add any element into this view then it gives me error.

当我从部分视图中删除 DIV 元素并只保留第一行(模型绑定)时,它没有给我错误,但是,每当我向此视图中添加任何元素时,它都会给我错误。

Please guide me for weird behavior.

请指导我的奇怪行为。

thanks

谢谢

回答by Saranga

You are using $.getJSONand in the controller you are returning a PartialView.

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

Change the $.getJSONto ajaxcall and set dataType: "html".

将 更改$.getJSONajaxcall 和 set dataType: "html"

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

回答by gaurav

Yes, try ajax post. instead of getJSON(). see full ajax configuration if above does not work. if still don't work please let me know.

是的,试试ajax post。而不是 getJSON()。如果以上不起作用,请参阅完整的 ajax 配置。如果仍然不起作用,请告诉我。

   $.ajax({ 
         url: "/ProductManagement/LoadProducts", 
         contentType:'application/html; charset=utf-8',
         type:'GET',
         dataType:'html',
                 success: function (evt) { 

                }, 
                error: function (req, status, error) { 

                } 
            });

回答by Dayan

You can use jQuery $.get()as following:

您可以使用 jQuery $.get()如下:

@section scripts
{
<script>
    $.get('/ProductManagement/LoadProducts', 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>

}