2011-07-01 103 views
6

我有一個局部視圖和int它,沒有從任何佈局的任何繼承的痕跡。但是,無論何時我想在視圖中使用它(渲染它),佈局都會重複一次以獲取視圖,並且一次獲取部分視圖。 This post建議創建一個空的佈局。但我認爲這是解決方法。無論如何停止加載部分視圖的佈局(主佈局)。我不明白,爲什麼當沒有代碼使用主佈局,爲什麼它應該被加載。這就像在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='[email protected]'>@padding.PadRight(level, '-') @childCategory.Title</option> 
      @RenderChildCategories(categories, level, childCategory.Id); 
    } 
    level--; 
} 
+0

可以顯示您的部分頁面的第一行以及您的控制器操作方法,從而覆蓋了此行爲? –

回答

13

在通過ajax cal渲染部分頁面時,我能夠重現此問題LS。

return View("partialpage") 

總會伴隨佈局。我通過明確調用

return PartialView("partialpage") 
+0

好習慣。我不知道PartialView是ActionResult類型之一。但是當你不使用ajax時你會做什麼,並且你想基於將某些部分視圖(如儀表板)放在一起來構建頁面。哪種方法? –

+1

根據模型的可用性,我使用RenderPartial或RenderAction –

9

佈局可能與您的~/Views/_ViewStart.cshtml

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

是未來你可以嘗試在你的局部視圖重寫此類似:

@{ 
    Layout = null; 
} 
+0

這對我有用。謝謝。但這不是很奇怪嗎?在WebForms中,它總是說「這個頁面沒有母版頁」,或者「這個用戶控件沒有母版頁」。我的意思是,似乎默認值應該是'Layout = null;'並且不應該有必要明確地說出來。 –

+0

WebForms視圖引擎使用不同的文件擴展名(.ascx表示部分,.aspx表示頁面)。另一方面,剃鬚刀對所有東西都使用相同的擴展名,所以這可能是原因(儘管如此,並非100%確定)。順便說一句,我不能重現你的問題。你怎麼稱呼你的部分?我試着@ Html.Partial(「_ Foo」),它只是工作。無需將佈局設置爲空。 –

+1

我用'@ Html.Action(「PartialViewControllerAction」)'。方法文檔說它返回部分視圖執行的渲染結果。 –