Using Static Variables in Razor

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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 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>

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x