问题
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?
I have researched google and got below link but which is also not useful.
Why does the PartialView keep calling itself?
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()
回答1:
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
...
@{
...
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)
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).
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.
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)
View (the "ProductMastetIndex.cshtml" file)
- PartialView (the "ProductAreaGrid.cshml" file)
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.
To resolve this issue, use the "Layout" directive in the "View" ("ProductMastetIndex.cshtml") file, not in the PartialView:
ProductAreaGrid.cshml:
@model IEnumerable<Brain.DAL.Entities.ProductArea>
@{
ViewBag.Title = "Product Area";
...
}
ProductMastetIndex.cshtml:
@{
ViewBag.Title = "Product Master";
Layout = "~/Views/Shared/_Layout.cshtml";
}
来源:https://stackoverflow.com/questions/57823284/partialview-action-is-calling-itself