Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

Friday, December 7, 2012

Setting up Session State in MVC

In Global.asax.cs add:

        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            if (HttpContext.Current.Session["SearchSettings"] == null)
            {
                HttpContext.Current.Session["SearchSettings"] = 
new SearchSettingsModel
                {
                    DefaultSearchKeyword = "Microsoft", 
                    QueryServiceUrl = "http://spsite/_vti_bin/search.asmx" };
            }
        }

Then in SearchController.cs put:
        public ActionResult SearchSettings()
        {
            var settingsModel = ControllerContext.HttpContext.Session["SearchSettings"] 
as SearchSettingsModel;

            return View(settingsModel);
        }

        [HttpPost]
        public ActionResult SearchSettings(SearchSettingsModel model,
 string returnUrl)
        {
            if (ModelState.IsValid)
            {

            }

            // If we got this far, something failed, redisplay form
            return RedirectToAction("SearchSettings"); 
//per http://blog.jorritsalverda.nl/2010/03/10/maintainable-mvc-post-redirect-get-pattern/
        }