【问题标题】:MVC ActionLink does not return PartialViewMVC ActionLink 不返回 PartialView
【发布时间】:2016-08-12 19:34:51
【问题描述】:

我正在尝试使用 Ajax 通过 PartialView 将信息返回到视图。

控制器有两种方法:

public ActionResult Deal()
{
    var product = getDeal();
    return PartialView("_deal",product);
}

private product getDeal()
{
    var product = db.Products.OrderByDescending(a => a.Name).First();
    product.Price *= 0.5m;
    return product;
}

这个控制器的 Index.cshtml 有这个部分:

<div id="deal" class="container">
@Ajax.ActionLink("Click here to see the daily deal!","Deal",
null,
new AjaxOptions
{
    UpdateTargetId = "deal",
    InsertionMode=InsertionMode.Replace,
    HttpMethod="GET"
},
new { @class = "btn btn-primary" }) 

Views 文件夹中有一个 PartialView _deal.cshtml,但 ActionResult Deal() 会重定向到 /Products/Deal URL,其中仅包含页面上 _deal.cshtml 中的数据。而不是渲染部分视图并使用 id="deal" 渲染 div 标签中的 html。

【问题讨论】:

    标签: asp.net-mvc asp.net-ajax


    【解决方案1】:

    您应该将 Microsoft.jQuery.Unobtrusive.Ajax 的 nuget 包添加到您的项目中。从包管理器控制台

    Search and install via NuGet Packages: Microsoft.jQuery.Unobtrusive.Ajax

    之后,您需要添加对视图的引用:

    @Scripts.Render("~/bundles/jquery")
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"> </script>
    

    您的“交易”ActionResult 可以是 PartialViewResult_deal.cshtml 应该放在 View > Shared 文件夹中。

    【讨论】:

    • 谢谢。我会尝试并报告。
    【解决方案2】:

    你可以像这样使用它

    将您的价值存储在模型中并传递您的模型。

    public ActionResult Deal()
            {
                return PartialView("../_ProductPartial",model)
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多