PartialView Action正在自我调用 [英] PartialView Action is calling itself

查看:53
本文介绍了PartialView Action正在自我调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MVC应用程序,该应用程序用于从主视图(ProductMaster)中将ProductAreaGrid的列表显示为PartialView,并且它将在Partialview中具有CreateProductArea作为PartialView。我的Gridview局部动作反复调用,我不确定为什么反复调用它。

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. My Gridview partial action is calling repeatedly and i am not sure why its getting called repeatedly. Is there any circular refrence in this code?

我研究了Google并获得了下面的链接,但它也没有用。

I have researched google and got below link but which is also not useful.

为什么PartialView不断调用自己?

下面是我的代码MVC代码。

Below is my code MVC code.

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);            
        }
 }

页面完全渲染后,下面的方法会反复调用。为什么会发生这种情况?

Once page is rendered completely, below method is calling repeatedly. Why does this happen?

public PartialViewResult PAGrid()


推荐答案


我在删除Layout =〜/ Views / Shared / _Layout.cshtml后发现了问题;

I figure out the problem after removing 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(不应引用布局)

最后一页/视图应包含完整的有效html内容:开始/结束html标签(在视图中或在相关布局中定义)。

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.

例如:

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

在您的方案中,最终布局可能以以下方式构造:

In your scenario, the final layout is likely constructed in the following manner:


  • 布局(〜/ Views / Shared / _Layout.cshtml文件)

  • Layout (the "~/Views/Shared/_Layout.cshtml" file)


  • 视图( ProductMastetIndex.cshtml文件)

  • View (the "ProductMastetIndex.cshtml" file)


  • PartialView( ProductAreaGrid.cshml文件)


但是我不确定为什么调用局部视图

but i am not sure why its calling the partial view

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

Assigning the "Layout" property in the PartialView seems to re-run the same rendering routine recursively starting with the Layout level.

要解决此问题,请在视图( ProductMastetIndex中使用 Layout指令。 cshtml)文件,而不是PartialView中的文件:

To resolve this issue, use the "Layout" directive in the "View" ("ProductMastetIndex.cshtml") file, not in the 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";
}

这篇关于PartialView Action正在自我调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆