c# - ASP.NET - PartialView 继续使用布局
问题描述
我有一个由几个步骤组成的结帐流程。出于性能原因,我使用 ajax 部分更新,因此我只想返回没有布局的部分视图。
请注意,我使用的是 ASP.NET Mvc,而不是 ASP.NET Core!
首先,我有一个 index 方法,它加载一个视图,然后将当前订单所在的步骤的一部分。
public override ActionResult Index(TradeInCheckOutPage currentPage)
{
var model = new BaseCheckoutStepViewModel(bulkOrderViewModel, currentPage,
GetCurrentStep(orderViewModel));
return View(Index.cshtml", model); // View handles redirect to correct view
}
该方法的观点:
if (Model.Step.Equals(CheckoutStep.Confirm))
{
Html.RenderAction("confirm", "CheckoutPage",
new
{
currentPageId = Model.CurrentPage.PageId,
});
}
else if (Model.Step.Equals(CheckoutStep.ChooseSenderAddress))
{
Html.RenderAction("chooseAddress", "CheckoutPage",
new
{
CurrentPage = Model.CurrentPage.PageId,
BulkOrderId = Model.BulkOrder.Id
});
}
订单处于“确认”状态,因此通过 Html.RenderAction 调用 Confirm 方法。
public ActionResult Confirm(Guid currentPageId)
{
var model = new CheckoutConfirmViewModel(null, GetCurrentPage(currentPageId));
return View("Confirm.cshtml", model);
}
该方法的视图,它启动一个局部的 ajax 调用:
@{
Layout = "_LayoutCheckOut.cshtml";
}
@using (Ajax.BeginForm(
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "content"
}))
{
}
我的控制器中有以下代码受到 ajax 调用的影响:
[HttpPost]
public ActionResult Confirm(Guid currentPageId, bool confirm = false)
{
if (ModelState.IsValid)
{
return chooseAddress(currentPageId, bulkOrder.Id);
}
public PartialViewResult chooseAddress(Guid currentPageId, Guid bulkOrderId)
{
...
return PartialView("ChooseAddress.cshtml", model);
}
问题是由于某种原因,虽然它是 PartialViewResult,但 chooseAddress 方法 viewresult 仍在使用布局!这是什么原因造成的?
我也尝试在 ChooseAddress 视图中指定 Layout = null ,但仍在呈现布局。
解决方案
尝试:
Html.RenderPartial("chooseAddress", "CheckoutPage",...
代替 Html.RenderAction("chooseAddress", "CheckoutPage",...
[ChildActionOnly]
还将属性添加到您的chooseAddress
操作中。这是一个很好的做法。
推荐阅读
- amazon-s3 - Gitlab-CI:AWS S3 部署失败
- c++ - cmake 的困境
- javascript - Angular - 如何按 Id 自动填写表格
- codenameone - 如何从图像中提取元数据
- flutter - Flutter - webRTC 视频通话信号不起作用
- c++ - 合并集合中满足谓词的元素
- python - 如何解决文件“
“python中的错误? - python - 更大的对数色标 Python sns.clustermap()
- java - 无法单击按钮 org.openqa.selenium.WebDriverException:在点 (502, 85) 处不可单击。其他元素会收到点击:
- c# - Visual Studio 中的 MSBuild - 在将文件包含为内容之前移动文件 (C#)