部分视图继承自主布局

Sae*_*ati 7 asp.net-mvc layout master-pages partial-views asp.net-mvc-3

我有一个局部视图和int it,没有任何布局的任何继承的痕迹.但是每当我想在视图中使用它(渲染它)时,布局会为视图重复一次,而对于局部视图则重复一次.这篇文章建议创建一个空的布局.但我认为这是解决方法.无论如何都要停止为部分视图加载布局(主布局).我不明白,为什么当没有代码使用主布局时,为什么要加载它.这就像在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--;
 }
Run Code Online (Sandbox Code Playgroud)

Muh*_*hid 17

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

return View("partialpage")   
Run Code Online (Sandbox Code Playgroud)

总是伴随着布局.我通过显式调用覆盖了这种行为

return PartialView("partialpage")  
Run Code Online (Sandbox Code Playgroud)


Dan*_*zzi 11

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

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

你可以尝试在局部视图中覆盖它,如:

@{
    Layout = null;
}
Run Code Online (Sandbox Code Playgroud)