2017-09-15 77 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