Reputation: 21
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.
Upvotes: 0
Views: 1965
Reputation: 2494
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]
Upvotes: 2
Reputation: 3051
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";
Upvotes: 0