0

I've declare a class for GlobalVarables in model.cs, in the same model there is also one more class which represent my table in the database.

namespace Project.Models
{
    [Table("REGIONS")]
    public class DBTable
    {
    // table columns{get;set;}
    }

    public static class MyViewModel
    {
        public static string vVar1{get; set;} 
        public static string vVar2{get; set;} 
    }

then papulate in Controller.

namespace Project.Controllers
{ 
    public class ProjectController : Controller
    {
        public ActionResult DisplayVariables(MyViewModel model)
        {

            model.Var1 = "testString";          
            return View(model);
        }

        ....
    }
}

index.cshtml code here

@model IEnumerable<sLightcSharp.Models.Region>

how can I include second model

@model IEnumerable<sLightcSharp.Models.MyViewModel>

and how can I use Var1 variable in index.cshtml.

kaka
  • 21
  • 5
  • what do you mean? you want to use that class in your views? – Sirwan Afifi Feb 19 '14 at 08:05
  • 1
    possible duplicate of [ASP.NET MVC Global Variables](http://stackoverflow.com/questions/5118610/asp-net-mvc-global-variables) – Andrei V Feb 19 '14 at 08:06
  • 1
    @AndreiV, actually this question is about how the variables __get used__. The OP, obviously, has declared them in a fashion similar to the proposed duplicate. – Ivaylo Slavov Feb 19 '14 at 08:09

2 Answers2

2

It depends weather these variables do change sometime in the application life cycle or not!? Let's say they are static and they don't change never, then you would have something like this

namespace Utilities
{
    public class Constants
    {
        public static readonly string DBCONTEXT_KEY = "dbcontext";
        public static readonly string COMPANY_ID = "COMPANY_ID";
        public static readonly string ESTABLISHMENT_ID = "ESTABLISHMENT_ID";
        public static readonly string USER_ID = "USER_ID";
    }
}

and the way you would call a variable in the view is pretty simple using razor syntax

@Utilities.Constants.COMPANY_ID

and the old way asp syntax would be

<%=Utilities.Constants.COMPANY_ID %>

but usually I use this kind of class in Session keys or some Dictionary keys like

HttpContext.Current.Items[Constants.DBCONTEXT_KEY]

or

HttpContext.Current.Session[Constants.USER_ID]
visar_uruqi
  • 2,484
  • 1
  • 23
  • 20
  • I am new to ASP.Net MVC... I have declare Constant class in model.cs but I cant able to use variable in index.cshtml like u did @Utility.... – kaka Feb 19 '14 at 09:24
  • Or please elaborate where I write the namespace/class in Controller or in Model or somewhere else – kaka Feb 19 '14 at 09:34
  • Utilities is a namespace, usually I put it in another project in Visual Studio but same solution file, or to be more accurate this is another class Library, but you can put this class also in a model.cs without a problem, just go to your class and look what namespace is your class or your project, if you don't have a namespace in your model.cs then you should have one, usually in models would be MyMVCApp.Models... or just go with your mouse right click on the Project then Properties and there is Default Namespace of your MVC project – visar_uruqi Feb 19 '14 at 11:38
0

What you could do is create a singleton, i use singletons in my websites for caching some basic stuff.

 public class Configuration
    {
        private static Configuration _me;

        public static Configuration Settings
        {
            get
            {
                if (_me == null)
                {
                    _me = new Configuration();
                }
                return _me;
            }
        }

        // Properties
        public string Title { get; set; }
        public string Description { get; set; }
   }
}

Then you can simply get/set the information by using:

Configuration.Settings.Variable = "Test";
Jamie
  • 3,031
  • 5
  • 36
  • 59
  • The OP asks how to use/reference his class in the .cshtml file. Your suggestion is an alternative to what s/he already implemented. Also, add a __private__ default constructor to your class, as it does not match the Singleton pattern. – Ivaylo Slavov Feb 19 '14 at 08:17