如何声明全局变量以使用所有mvc视图? [英] How can i declare global variable to use all mvc view?

查看:102
本文介绍了如何声明全局变量以使用所有mvc视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有5个视图,我想声明1个变量(全局),5个视图可以使用该变量。

全局变量可以存储列表客户(客户代码和客户名称)。

注意:

当用户登录时,我想将客户信息分配给该变量,这些值将用于5次浏览



谢谢

Hi all,
I have 5 views and i want to declare 1 variable (global) that 5 views can use that variable.
That global variable can store list of customer (Customer Code and Customer Name).
Note:
When user log in, i want to assign customer info to that variable and those value will be use in in 5 views

Thanks

推荐答案

使用全局变量通常是一件坏事,它通常是对可重用性的。你想要的是在5个不同的对象之间共享数据。然后创建数据对象并将此数据对象的引用传递给所有需要它的对象,此外,您也可以为自己保留引用。您可以通过对其进行引用来解决对象的破坏。
Using global variables is often a bad thing, its usually against reusability. What you want is to share data between 5 different objects. Then create your data object and pass the reference of this data object to all objects that need it and additionally you can keep a reference for yourself too. You can solve the destruction of the object by making it reference counted.


您有几个选择,但不要将其视为变量,将其视为状态。当您开始将其视为状态时,您可以开始询问有关存储,操作和/或访问的问题。



您可以始终使用会话状态。您可以从任何控制器或视图访问它。默认情况下,这会存储在内存中,并在用户会话终止和/或服务器上的应用程序池重置时丢失。它与单个用户绑定。



You have a couple of choices, but don't think of it as a variable, think of it as state. When you start thinking of it as state, you can start asking questions around storage, manipulation and/or access.

You could always use session state. You can access this from any controller or view. By default, this is stored in memory and lost when a user's session dies and/or your application pool is reset on the server. It is tied to a single user.

Session["YourVariable"] = "Customer Name";











or

Session["Customers"] = new List<customer>();</customer>





或类似。



更好的是存储这个信息在数据库中。网站和网络上有很多资源。



干杯。



or similar.

Better would be to store this information in a database. There are many resources for this here on the site and across the web.

Cheers.


这篇关于如何声明全局变量以使用所有mvc视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆