我有MVC应用程序,该应用程序用于从主视图(ProductMaster)中将ProductAreaGrid的列表显示为PartialView,它将在Partialview中具有CreateProductArea作为PartialView。我的Gridview局部动作反复调用,我不确定为什么反复调用它。该代码中是否有任何循环参考?
我研究了谷歌,并得到下面的链接,但也没有用。
Why does the PartialView keep calling itself?
下面是我的代码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="#[email protected]" data-toggle="modal">Edit</a>
<a href="#" class="btn btn-xs btn-danger" data-target="#[email protected]" 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()
最佳答案
删除Layout =“〜/ Views / Shared / _Layout.cshtml”之后,我发现了问题;
这是一个原因。在此处指定“布局”属性/指令:
ProductAreaGrid.cshml
...
@{
...
Layout = "~/Views/Shared/_Layout.cshtml";
}
通常,最终页面/视图以以下嵌套结构呈现:
布局
查看(引用布局)
PartialView(s)(不应引用布局)
最终页面/视图应包含完整的有效html内容:开始/结束html标签(在视图中或相关布局内定义)。
部分视图-是一个块/单元,不应带有自己的开始/结束html标签,而应提供整个html内容的一部分。
例如:
<!--Layout-->
<html>
...
<body>
<!--View-->
<!--PartialView-->
<!--PartialView-->
<!--View-->
</body>
</html>
<!--Layout-->
在您的方案中,最终布局可能以以下方式构造:
布局(“〜/ Views / Shared / _Layout.cshtml”文件)
查看(“ ProductMastetIndex.cshtml”文件)
PartialView(“ ProductAreaGrid.cshml”文件)
但我不确定为什么它调用了局部视图
在PartialView中分配“ Layout”属性似乎从Layout级别开始递归地重新运行相同的渲染例程。
要解决此问题,请在“视图”(“ ProductMastetIndex.cshtml”)文件中而不是在PartialView中使用“布局”指令:
ProductAreaGrid.cshml:
@model IEnumerable<Brain.DAL.Entities.ProductArea>
@{
ViewBag.Title = "Product Area";
...
}
ProductMastetIndex.cshtml:
@{
ViewBag.Title = "Product Master";
Layout = "~/Views/Shared/_Layout.cshtml";
}
关于c# - PartialView Action正在自我调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57823284/