c# - Razor MVC3 在返回 View 时保持全局变量集

标签 c# asp.net-mvc

我有一个全局字符串变量,我在执行一个操作(按下提交按钮)后设置,然后我想在按下另一个按钮时访问同一个字符串,目前我正在做类似的事情

GlobalVariable = "blah";
return View();

再次访问它的最佳做法是什么。我想指出这是同一页(index.cshtml)

谢谢!

最佳答案

如果它是每个用户的值,请使用:

Session["MyKey"] = "MyValue"; // save the value
var myValue = (string) Session["MyKey"]; // retrieve the value

如果所有用户的一个值使用这个:

Application["MyKey"] = "MyValue"; // save the value
var myValue = (string) Application["MyKey"]; // retrieve the value

希望这对您有所帮助。

关于c# - Razor MVC3 在返回 View 时保持全局变量集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426627/

相关文章:

c# - Mono c# TextRenderingHint.SingleBitPerPixelGridFit Windows 与 Linux

c# - 如何在 Fluent NHibernate 上设置 SysCache?

c# - 将 json 数组反序列化为 C# 列表时遇到问题

c# - 如何将 Display 属性添加到外部枚举?

c# - ASP.NET Identity 2 Invalidate Identity难点

c# - DocumentDB - 输入名称无效。确保提供少于 '255' 个字符的唯一非空字符串

c# - Java 中的 DataTable 等价物

c# - 正则表达式查找 = 之后的字符串

c# - 执行 SaveChanges 时排除未修改的字段

jquery - MVC4 Ajax 在 Windows 滚动上加载记录