部分视图继承自主布局
我有一个局部视图和 int 它,没有任何布局的任何继承的痕迹。但是每当我想在视图中使用它(渲染它)时,布局就会为视图重复一次,为部分视图重复一次。 这篇文章建议创建一个空布局。但我认为这是解决方法。无论如何,是否可以停止加载部分视图的布局(主布局)。我不明白,为什么当没有代码使用主布局时,为什么要加载它。这就像在 ASP.NET 中创建一个页面并看到它继承自母版页而没有 <%@ Master ...
指令一样。
这是我的部分看法:
@* Recursive category rendering *@
@using Backend.Models;
@{
List<Category> categories = new ThoughtResultsEntities().Categories.ToList();
int level = 1;
}
@RenderCategoriesDropDown(categories, level)
@helper RenderCategoriesDropDown(List<Category> categories, int level)
{
List<Category> rootCategories = categories.Where(c => c.ParentId == null).ToList();
<select id='categoriesList' name='categoriesList'>
@foreach (Category rootCategory in rootCategories)
{
<option value='@rootCategory.Id' class='level-1'>@rootCategory.Title</option>
@RenderChildCategories(categories, level, rootCategory.Id);
}
</select>
}
@helper RenderChildCategories(List<Category> categories, int level, int parentCategoryId)
{
string padding = string.Empty;
level++;
List<Category> childCategories = categories.Where(c => c.ParentId == parentCategoryId).ToList();
foreach (Category childCategory in childCategories)
{
<option value='@childCategory.Id' class='level-@level'>@padding.PadRight(level, '-') @childCategory.Title</option>
@RenderChildCategories(categories, level, childCategory.Id);
}
level--;
}
I have a partial view and int it, there is no trace of any inheritance from any layout. But whenever I want to use it (render it) inside a view, the layout gets repeated once for the view, and once for the partial view. This post suggests to create an empty layout. But I think this is the workaround. Is there anyway to stop loading layout (master layout) for partial views. I don't understand, why when there is no code to use the master layout, why should it get loaded. It's just like creating a page in ASP.NET and seeing that it inherits from a master page without having <%@ Master ...
directive.
This is my partial view:
@* Recursive category rendering *@
@using Backend.Models;
@{
List<Category> categories = new ThoughtResultsEntities().Categories.ToList();
int level = 1;
}
@RenderCategoriesDropDown(categories, level)
@helper RenderCategoriesDropDown(List<Category> categories, int level)
{
List<Category> rootCategories = categories.Where(c => c.ParentId == null).ToList();
<select id='categoriesList' name='categoriesList'>
@foreach (Category rootCategory in rootCategories)
{
<option value='@rootCategory.Id' class='level-1'>@rootCategory.Title</option>
@RenderChildCategories(categories, level, rootCategory.Id);
}
</select>
}
@helper RenderChildCategories(List<Category> categories, int level, int parentCategoryId)
{
string padding = string.Empty;
level++;
List<Category> childCategories = categories.Where(c => c.ParentId == parentCategoryId).ToList();
foreach (Category childCategory in childCategories)
{
<option value='@childCategory.Id' class='level-@level'>@padding.PadRight(level, '-') @childCategory.Title</option>
@RenderChildCategories(categories, level, childCategory.Id);
}
level--;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 ajax 调用渲染部分页面时,我能够重现此问题。总是
伴随着布局。我已经通过显式调用覆盖了此行为
I was able to reproduce this issue when rendering partial pages through ajax calls. The
would always accompany with layout. I have overridden this behavior by explicitly calling
布局可能来自您的
~/Views/_ViewStart.cshtml
您可以尝试在部分视图中覆盖它,例如:
The layout might be coming from your
~/Views/_ViewStart.cshtml
You could try overriding this in your partial view like: