部分视图继承自主布局

发布于 2024-11-18 10:11:38 字数 1652 浏览 1 评论 0原文

我有一个局部视图和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2024-11-25 10:11:38

通过 ajax 调用渲染部分页面时,我能够重现此问题。总是

return View("partialpage")   

伴随着布局。我已经通过显式调用覆盖了此行为

return PartialView("partialpage")  

I was able to reproduce this issue when rendering partial pages through ajax calls. The

return View("partialpage")   

would always accompany with layout. I have overridden this behavior by explicitly calling

return PartialView("partialpage")  
二手情话 2024-11-25 10:11:38

布局可能来自您的 ~/Views/_ViewStart.cshtml

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

您可以尝试在部分视图中覆盖它,例如:

@{
    Layout = null;
}

The layout might be coming from your ~/Views/_ViewStart.cshtml

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

You could try overriding this in your partial view like:

@{
    Layout = null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文