【问题标题】:Calling Partial View Using JQuery使用 JQuery 调用局部视图
【发布时间】:2021-10-15 03:09:51
【问题描述】:

我的/Shared 文件夹中有一个像这样定义的局部视图

<div id="myProducts">
  @Html.Partial("_ProductsList",Model)
 </div>

我想通过 JQuery 加载 _ProductsList。基本上,我试图在按钮单击时刷新我的_ProductsList 视图。我试着这样做:

$('myProducts').Load(/Shared/_ProductsList)

但我收到“找不到资源错误”。

【问题讨论】:

  • A previous question 提出了不同的问题,one of the answers 的问题演示了如何通过 AJAX 加载部分内容。 (免责声明:我自己发布了该问题的答案——尽管我的答案实际上并没有解决您的问题;其他人可能会。)
  • 除此之外,/Shared/_ProductsList 应该在你的 jQuery 中用引号引起来。我认为这是一个错字。另外,您能否确认您可以导航到/Shared/_ProductsList 并得到回复?这将有助于确认这不是路由问题。
  • 你需要在控制器中创建一个返回/Shared/_ProductsList局部视图的方法。您可以像处理其他视图一样从 jquery 调用此方法

标签: javascript c# jquery asp.net-mvc asp.net-mvc-4


【解决方案1】:

尝试使用这个 ajax 代码

        $.ajax({
        
            url: "/MyController/MyAction",
            type: "POST",
            success: function (result) {
                    $("#myProducts").html(result);
            },
            error: function (xhr, exception) {
               
            }
        });

和行动

public partial class MyController : Controller
{
  public IActionResult MyAction()
  {
      var model=new myModel { ... assign model properties  };
     return PartialView("_ProductsList", model);
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2021-03-14
    • 2019-12-22
    相关资源
    最近更新 更多