PartialView Action 正在调用自身

PartialView Action is calling itself

我有 MVC 应用程序,它用于从主视图 (ProductMaster) 将 ProductAreaGrid 列表显示为 PartialView,并且它将在局部视图中将 CreateProductArea 作为 PartialView。我的 Gridview 部分操作重复调用,我不确定为什么它被重复调用。这段代码有没有循环引用?

我研究了 google 并低于 link,但这也没有用。

下面是我的代码 MVC 代码。

ProductAreaGrid.cshml

@model IEnumerable<Brain.DAL.Entities.ProductArea>
@{
    ViewBag.Title = "Product Area";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>
    <a href="#" class="btn btn-success" data-target="#CreateProductArea" data-toggle="modal">
        Add New
        <i class="fa fa-plus"></i>
    </a>
</p>
@Html.Partial("Partials/PA/CreateProductArea", null, new ViewDataDictionary() {})
<div class="table-responsive">
    <table class="table table-bordered table-hover dataTable gray-table">
        <thead>
            <tr>
                <th>Action</th>
                <th>Name</th>
                <th>Is Active</th>
            </tr>
        </thead>
        <tbody>
            @if (!Model.Any())
            {
                <tr>
                    <td colspan="3">There are no required support entries.</td>
                </tr>
            }
            else
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>
                            <a href="#" class="btn btn-xs btn-success" data-target="#EditReportLink-@item.Id" data-toggle="modal">Edit</a>
                            <a href="#" class="btn btn-xs btn-danger" data-target="#DeleteReportLink-@item.Id" data-toggle="modal">Deactivate</a>
                            @Html.Partial("Partials/PA/EditProductArea", item)
                                @Html.Partial("Partials/PA/De_ActivateProductArea", item.Id)
                        </td>
                        <td>
                            @Html.DisplayFor(model => item.Name)
                        </td>
                        <td>@Html.DisplayFor(model => item.IsActive)</td>
                    </tr>
                }
            }
        </tbody>
    </table>
</div>

ProductMastetIndex.cshtml

@{
    ViewBag.Title = "Product Master";
}

@section Breadcrumb {
    <ul class="breadcrumb">
        <li>
            <a href="@Url.Action("index", "home" )">Dashboard</a>
        </li>
        <li class="active">
            <span>@ViewBag.Title </span>
        </li>
    </ul>
}
@section Scripts {
    <script>

    </script>
}

<div class="clearfix"></div>

@Html.Partial("ValidationSummary", ViewData.ModelState)
<div>
    <br class="visible-sm visible-xs" />
    <h3 class="tab-title">Product Area</h3>
    <hr />
    <div class="row">
        <div class="col-lg-8">
            @Html.Partial("AjaxGrid", Url.Action("PAGrid"), new ViewDataDictionary() { })
        </div>
    </div>
    <hr class="seperate-line">
</div>
<div class="clearfix"></div>

ProductMasterController.cs

 public class ProductMasterController : BaseController
    {
        private CachedCollections _cachedCollections;
        private ProjectHelper _projectHelper;
        private IUsersService _usersServices;

        [SCIAuthorize(RoleEnum.PMO)]
        [HttpGet]
        public ActionResult ProductMasterIndex()
        {
            try
            {                                                
                return View();
            }
            catch (Exception ex)
            {
                LogError(ex);
                return Json(new { Message = new ToastrMessage(null, (ex is BrainServiceException) ? ex.Message : AppGlobalMessages.UnexpectedErrorMessage, ToastrMessageTypeEnum.Error) });
            }            
        }


        #region Product Area

        [SCIAuthorize(RoleEnum.PMO)]        
        public PartialViewResult PAGrid()
        {
            var collection = _db.GetProductAreas()
                .AsNoTracking()
                .ToList();
            return PartialView("Partials/PA/PAGrid", collection);            
        }
 }

页面完全呈现后,以下方法将重复调用。为什么会这样?

public PartialViewResult PAGrid()

I figure out the problem after removing Layout = "~/Views/Shared/_Layout.cshtml";

这是一个原因。 "Layout" property/directive 在这里指定:

ProductAreaGrid.cshml

...
@{
    ...
    Layout = "~/Views/Shared/_Layout.cshtml";
}

通常情况下,最终的 page/view 呈现在以下嵌套结构中:

  • 布局

    • 视图(引用布局)

      • 部分视图(不应引用布局)

最后的 page/view 应该包含完整有效的 html 内容:start/end html 标签(在视图中或相关布局中定义)。

局部视图 - 是一个 block/unit,不应带有自己的 start/end html 标签,但应提供整个 html 内容的一部分。

例如:

<!--Layout-->
<html>
  ...
  <body>
      <!--View-->
          <!--PartialView-->
          <!--PartialView-->
      <!--View-->
  </body>
</html>
<!--Layout-->

在您的场景中,最终布局可能按以下方式构建:

  • 布局(“~/Views/Shared/_Layout.cshtml”文件)

    • 查看("ProductMastetIndex.cshtml" 文件)

      • PartialView("ProductAreaGrid.cshml" 文件)

but i am not sure why its calling the partial view

在 PartialView 中分配 "Layout" 属性 似乎重新运行 相同的渲染例程递归地从布局级别开始。

要解决此问题,请在 "View" ("ProductMastetIndex.cshtml") 文件中使用 "Layout" 指令,而不是在 PartialView 中:

ProductAreaGrid.cshml:

@model IEnumerable<Brain.DAL.Entities.ProductArea>
@{
    ViewBag.Title = "Product Area";
    ...
}

ProductMastetIndex.cshtml:

@{
    ViewBag.Title = "Product Master";
    Layout = "~/Views/Shared/_Layout.cshtml";
}