Using Static Variables in Razor

寵の児 提交于 2019-12-29 05:44:06

问题


Why is it not possible to use a static Variable from a static class inside a view?

For example, lets say you have a Settings Class:

public static class GlobalVariables
{
    public static string SystemColor
    {
        get { return Properties.Settings.Default.SystemColor; }
    }
}

Why wouldn't you be able to call it in a view?

like so

@using AppName.Models
<html>
<div ><h1 style="color:@GlobalVariables.SystemColor">System Color</h1></div>
</html>

回答1:


As far as I'm aware, you can access static variables from inside a view in ASP.NET MVC, if you include the class' namespace with the appropriate using statement:

@using WhateverNamespaceGlobalVariablesIsIn

More importantly, you shouldn't be accessing static variables directly from views anyway. In keeping with the MVC pattern, all of your view's data should be accessible in your view model:

public ActionResult MyAction()
{
    var model = new MyViewModel();
    model.SystemColor = GlobalVariables.SystemColor;
    ...
    return View(model);
}

View:

@model MyViewModel

<div>
    <h1 style="color:@(Model.SystemColor)">System Color</h1>
</div>

If you need to do this in your layout file, you can use RenderAction to call a controller action and return a partial view instead. The partial can then be typed to MyViewModel, which can be used as above.




回答2:


your global class should be like

public class GlobalVariables
{
    public static string SystemColor
    {
        get { return Properties.Settings.Default.SystemColor; }
    }
}

and in page @AppName.GlobalVariables.SystemColor appname replace by namespace of global class

@using AppName.Models
<html>
<div ><h1 style="color:@AppName.GlobalVariables.SystemColor">System Color</h1></div>
</html>
</p>



回答3:


You can access static variables in the view. There are three ways of doing this:

1) As Ant P suggests, include using statement in the view. Given that the namespace of the GlobalVariables class is AppName.GlobalVariables:

@using AppName.GlobalVariables
<html>
    <div ><h1 style="color:@GlobalVariables.SystemColor">System Color</h1></div>
</html>

2) Another way is to directly use the namespace in the line where you want to access variable:

<div ><h1 style="color:@AppName.GlobalVariables.SystemColor">System Color</h1></div>

3) Finally, you can add needed namespace to the web.config file under Views folder:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Routing" />
      <add namespace="System.Web.Optimization" />
      <add namespace="AppName.GlobalVariables" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

As for the sticking the variable in the Model and passing it to the View from there... indeed it conforms to the MVC pattern and assures separation of concerns and all that goodness. But in my opinion in some cases "sticking to the pattern" is taken to the level of absurd. In your case I'd just access this variable from the view.



来源:https://stackoverflow.com/questions/17271827/using-static-variables-in-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!