2011-09-30 24 views
2

我試圖加載在asp.net MVC3應用程序中的視圖特定的樣式表(剛學這個東西!),所以在我_Layout.cshtml我:在asp.net mvc3應用程序中加載視圖特定的樣式表?

<head> 
<!--- All the common css & JS declarations here --> 
@ViewBag.PageIncludes 
</head> 
<body> 

然後在我的控制,我有:

public ActionResult Index() 
     { 
      ViewBag.PageIncludes = "<link rel='stylesheet' type='text/css' href='../Content/viewspecificstylesheet.css')' />"; 
      return View(); 
     } 

然而,當我查看該頁面,即使聲明是在頭部,文字呈現在人體內,從而呈現爲文本。

幾個問題的結果:

爲什麼,即使我的頭是這樣呈現在體內申報的嗎? 加載給定視圖/控制器的特定樣式表的最佳做法是什麼?

感謝

回答

4

您可以使用部分:

<head> 
    <!--- All the common css & JS declarations here --> 
    @RenderSection("Styles", false) 
</head> 
<body> 
... 
</body> 

然後在Index.cshtml觀點:

@section Styles { 
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/viewspecificstylesheet.css")" />  
} 

<div>This is the index view</div> 

和控制器不再需要擔心的風格是純粹查看特定責任:

public ActionResult Index() 
{ 
    return View(); 
} 
+0

完美。感謝那。我認爲有更好的方法可以從控制器中刪除視圖特定的數據。發揮魅力。謝謝...:) – LDJ

相關問題