2017-09-15 47 views
0

我正在使用javascript在ASP.net MVC應用程序中工作。我正在使用devExpress dxSwitch嘗試更改頁面的主題。DevExpress獲取樣式表添加變量

我在我的_layout.cshtml的頭這段代碼

@Html.DevExpress().GetStyleSheets("dark", 
    new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.Editors }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.Dashboard} 
) 

而不是硬編碼在我想看看我是否能代替它使用一個變量,並應用了主題爲「暗」這個功能可以在明暗之間切換。

$('#switch').click(function() { 


      var whichTheme = $("#switch").dxSwitch("instance"); 
      var valueSwitch = whichTheme.option("value"); 
      if (valueSwitch === true) { 

       $('body').css('background-color', '#343434'); 

      } else { 

       $('body').css('background-color', 'whitesmoke'); 
      } 
     }); 

回答

0

我想你沒有機會這樣做。 Javascript和Razor不能溝通,因爲渲染髮生在jQuery開始工作之前。

我會從DevExpress中查看HTML,然後在元素本身或頁面上的樣式標記中添加或更改樣式。

0

看來你想要的東西是這樣的https://codecentral.devexpress.com/E3825/Home/PostTheme,你可以做到這一點,但它並不像你想象的那麼簡單,請查看此示例代碼

namespace MvcApp_Theme { 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication { 
     public static void RegisterGlobalFilters(GlobalFilterCollection filters) { 
      filters.Add(new HandleErrorAttribute()); 
     } 

     public static void RegisterRoutes(RouteCollection routes) { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 

     } 

     protected void Application_Start() { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
     } 

     protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) { 
      DevExpressHelper.Theme = Utils.CurrentTheme; 
     } 
    } 
} 

你這裏有整個代碼在這個環節https://www.devexpress.com/Support/Center/Example/Details/E3825/how-to-change-a-theme-on-the-fly