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.

3
  • what do you mean? you want to use that class in your views? Commented Feb 19, 2014 at 8:05
  • 1
    possible duplicate of ASP.NET MVC Global Variables Commented Feb 19, 2014 at 8: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. Commented Feb 19, 2014 at 8:09

2 Answers 2

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] 
Sign up to request clarification or add additional context in comments.

3 Comments

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....
Or please elaborate where I write the namespace/class in Controller or in Model or somewhere else
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
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"; 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.