Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial view inherits from master layout

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--;
 }
like image 925
Saeed Neamati Avatar asked Jul 01 '11 10:07

Saeed Neamati


2 Answers

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")  
like image 164
Muhammad Adeel Zahid Avatar answered Nov 05 '22 08:11

Muhammad Adeel Zahid


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;
}
like image 31
Daniel Liuzzi Avatar answered Nov 05 '22 08:11

Daniel Liuzzi