简体   繁体   English

PartialView Action 正在调用自己

[英]PartialView Action is calling itself

I have MVC application, which is used to display the list of ProductAreaGrid as PartialView from the main view (ProductMaster) and it will have CreateProductArea as PartialView inside the partialview.我有 MVC 应用程序,它用于将 ProductAreaGrid 的列表显示为来自主视图(ProductMaster)的 PartialView,并且它将在局部视图中将 CreateProductArea 作为 PartialView。 My Gridview partial action is calling repeatedly and i am not sure why its getting called repeatedly.我的 Gridview 部分操作重复调用,我不确定为什么它被重复调用。 Is there any circular refrence in this code?这段代码中是否有任何循环引用?

I have researched google and got below link but which is also not useful.我已经研究过谷歌并获得了以下链接,但这也没有用。

Why does the PartialView keep calling itself? 为什么 PartialView 不断调用自己?

Below is my code MVC code.下面是我的代码MVC代码。

ProductAreaGrid.cshml 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 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 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);            
        }
 }

Once page is rendered completely, below method is calling repeatedly.一旦页面完全呈现,下面的方法就会重复调用。 Why does this happen?为什么会发生这种情况?

public PartialViewResult PAGrid()

I figure out the problem after removing Layout = "~/Views/Shared/_Layout.cshtml";我在删除 Layout = "~/Views/Shared/_Layout.cshtml" 后找出问题;

This is a cause.这是一个原因。 The "Layout" property/directive is specified here: “布局”属性/指令在此处指定:

ProductAreaGrid.cshml ProductAreaGrid.cshml

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

Normally, the final page/view is rendered in the following nested structure:通常,最终页面/视图以以下嵌套结构呈现:

  • Layout布局

    • View (references a layout)视图(引用布局)

      • PartialView(s) (should not reference the layout) PartialView(s)(不应引用布局)

The final page/view should contain a full valid html content: the start/end html tag (either defined in the view or within a related layout).最终页面/视图应包含完整的有效 html 内容:开始/结束 html 标记(在视图中或相关布局中定义)。

A partial view - is a block/unit that should not bring its own start/end html tag, but provide a part of the entire html content.局部视图 - 是一个块/单元,不应带自己的开始/结束 html 标签,但提供整个 html 内容的一部分。

For example:例如:

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

In your scenario, the final layout is likely constructed in the following manner:在您的场景中,最终布局可能按以下方式构建:

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

    • View (the "ProductMastetIndex.cshtml" file)查看(“ProductMastetIndex.cshtml”文件)

      • PartialView (the "ProductAreaGrid.cshml" file) PartialView(“ProductAreaGrid.cshml”文件)

but i am not sure why its calling the partial view但我不知道为什么它调用局部视图

Assigning the "Layout" property in the PartialView seems to re-run the same rendering routine recursively starting with the Layout level.在 PartialView 中分配“布局”属性似乎从布局级别开始递归地重新运行相同的渲染例程。

To resolve this issue, use the "Layout" directive in the "View" ("ProductMastetIndex.cshtml") file, not in the PartialView:要解决此问题,请在“视图”(“ProductMastetIndex.cshtml”)文件中使用“布局”指令,而不是在 PartialView 中:

ProductAreaGrid.cshml: ProductAreaGrid.cshml:

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

ProductMastetIndex.cshtml: ProductMastetIndex.cshtml:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM