I have a simple requirement where I have 2 date values which I will be using in my view(hardcoded right now) to be read from Web config file. I got some inputs saying these values have to be passed to controller, put them in a view bag, and then read in View. Since this is the 1st time I am working on MVC, please help me with how to proceed with this. Detailed code in each of the places( Web config, controller, and View) would be helpful.
Thanks in advance, Ad开发者_开发技巧arsh
Configure Unity as IOC by creating your own ControllerFactory http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx
Then at start-up register your web.config settings with the unity container in an object of your setting class and your controllers can then take it in constructor and it will automatically be injected.
public class HomeController : Controller
{
WebConfigSettings settings;
public HomeController(WebConfigSettings settings) // <-- coming via IOC
{
this.settings = settings;
}
public ActionResult Index()
{
ViewBag.SomeSetting = settings.SomeSetting;
return View();
}
}
You can then set the values from that in your view bag easily.
精彩评论