2015-10-27 18 views
1

我有一个MVC应用程序,具有_Layout.cshtml页面,如下所示(只显示相关部分):MVC - 添加/删除样式当用户到达,或者树叶,主页

<body> 
    <div id="wrapper"> 
     <div class="navbar navbar-inverse navbar-fixed-top container" style="background-image: url(/Images/HeaderBar.png); background-size: cover;"> 
      <div class="container"> 
       <div class="navbar-header"> 

当用户离开主页时,我想让您在div中看到的样式出现。但是,当用户到达主页时,我希望删除该样式。有没有一个干净和简单的方法来做到这一点?

回答

2

在您的_Layout.cshtml中,您可以检查ViewContext.RouteData["action"]ViewContext.RouteData["controller"]ViewContext.Controller.GetType()值并有条件地执行某些操作。

例如:

<div class="navbar navbar-inverse navbar-fixed-top container" id="sidebar" style="@(ViewContext.Controller.GetType() == typeof (HomeController) ? "background-image: url(/Images/HeaderBar.png); background-size: cover;" : "")"> 
    <p> some stuff here</p> 
</div> 

我会还建议使用一个类来完成,而不是内嵌样式此。

+0

优秀!!很棒。 –