首页 > 解决方案 > 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 ,但仍在呈现布局。

标签: c#asp.netasp.net-mvc

解决方案


尝试:

Html.RenderPartial("chooseAddress", "CheckoutPage",...

代替 Html.RenderAction("chooseAddress", "CheckoutPage",...

[ChildActionOnly]还将属性添加到您的chooseAddress操作中。这是一个很好的做法。


推荐阅读