【发布时间】: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。
【问题讨论】: