25

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>

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

7 Comments

So you shouldn't because of conforming to the MVC pattern? is that the main reason why?
As far as I know, you can use static variables in views as long as you include the namespace of the class with @using. You shouldn't, though.
Alright, that makes sense, I just didn't want to have to go create viewmodels for everything even when I wasn't passing a viewmodel. Like for example I have a customer list that just passed a list of customer model.
If it is something like a global color, as in the question, what is the point of going through all of the extra work just to expose it in the model? How is that better than using the static?
I'm with this, seems silly and over the top to pass it in a variable - and we are discouraged of making 'magic' numbers/variables.
|
15

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>

Comments

5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.