C# 在 Razor 中使用静态变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17271827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:00:00  来源:igfitidea点击:

Using Static Variables in Razor

c#asp.netasp.net-mvc-4razor

提问by clifford.duke

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>

采纳答案by Ant P

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

据我所知,如果您使用适当的语句包含类的命名空间,您可以从 ASP.NET MVC 的视图内部访问静态变量using

@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:

更重要的是,无论如何您都不应该直接从视图访问静态变量。为了与 MVC 模式保持一致,您的所有视图数据都应该可以在您的视图模型中访问:

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 RenderActionto call a controller action and return a partial view instead. The partial can then be typed to MyViewModel, which can be used as above.

如果您需要在布局文件中执行此操作,您可以使用RenderAction调用控制器操作并返回部分视图。然后可以将部分输入到MyViewModel,可以如上使用。

回答by sangram parmar

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 appnamereplace by namespace of global class

并在页面@ AppName.GlobalVariables.SystemColor appname替换为全局类的命名空间

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

回答by Dmitry Efimenko

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 GlobalVariablesclass is AppName.GlobalVariables:

1) 正如 Ant P 所建议的,在视图中包含 using 语句。鉴于GlobalVariables该类的命名空间是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:

2)另一种方法是直接使用要访问变量的行中的命名空间:

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

3) 最后,您可以将所需的命名空间添加到 Views 文件夹下的 web.config 文件中:

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

至于将变量粘贴在模型中并将其从那里传递给视图......确实它符合 MVC 模式并确保关注点分离和所有这些优点。但在我看来,在某些情况下,“坚持模式”被认为是荒谬的。在你的情况下,我只是从视图中访问这个变量。