asp.net-mvc - 部分 View 继承自主布局

标签 asp.net-mvc asp.net-mvc-3 layout master-pages partial-views

我有一个局部 View 和 int 它,没有任何布局的任何继承痕迹。但是每当我想在 View 中使用它(渲染它)时,布局都会为 View 重复一次,为局部 View 重复一次。 This post建议创建一个空布局。但我认为这是解决方法。无论如何停止加载部分 View 的布局(主布局)。我不明白,为什么当没有使用主布局的代码时,为什么要加载它。这就像在 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--;
 }

最佳答案

通过ajax调用呈现部分页面时,我能够重现这个问题。这

return View("partialpage")   

总是伴随着布局。我通过显式调用覆盖了这种行为
return PartialView("partialpage")  

关于asp.net-mvc - 部分 View 继承自主布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6546517/

相关文章:

css - 位置为 : relative 的同位素网格项

qt - 从 QT 布局中删除 qwidget

asp.net-mvc - 用于将错误添加到模型状态 MVC 验证的自动映射器映射

asp.net - 有没有办法确定 ASP.Net MVC Bundle 是否在之前呈现?

c# - Html.EditorFor 设置默认值

asp.net-mvc - Razor Method Chaining/Fluent 不适用于换行符

c# - 意外的模型绑定(bind)

asp.net-mvc-3 - MVC 中动态字段的验证

asp.net-mvc-3 - ASP.net MVC 3 更新或删除

影响外部 <div> 的 CSS margin-top