locked
State Management in MVC RRS feed

  • Question

  • User-791721227 posted

    Hello Friends,

    I am new to MVC.I have worked on Web form.

    In web form,session variable was used to carry data from multiple pages.

    In MVC,,we used viewdata and viewbag to carry data from controller to view.

    Is session variable  not used in MVC.

    To carry data in multiple pages what is used in MVC

    Wednesday, May 19, 2021 6:51 AM

Answers

  • User1686398519 posted

    Hi smitapinky2021, 

    Is session variable  not used in MVC.

    If it is an ASP.NET MVC project, you can use Session like this:

    //Save to session state in the action method or view
    Session["test"] ="testdata";
    //Read from session state in the action method or view
    var test = Session["test"];

    If it is an ASP.NET Core MVC project, you can use Session like this:

    1. To enable the session middleware, Startup must contain:

      1. Any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session.
      2. A call to AddSession in ConfigureServices.
      3. A call to UseSession in Configure.
    2. After configuring the session state, you can set and get the session value as follows:
      • // Requires: using Microsoft.AspNetCore.Http;
        if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
        {
            HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        }
        var name = HttpContext.Session.GetString(SessionKeyName);
    3. Session state

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2021 8:13 AM

All replies

  • User753101303 posted

    Hi,

    Web Forms and MVC are two UI presentation model but behind the scene you still have all the underlying genrral ASP.NET framework

    Passing data from the controller to the view is done during the same request. It''s basically the same thing than data binding if you used that in Web Forms.

    Behind the scene it is still ASP.NET you still jhave all the general options You also have https://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc intended to be used forr passing data from one page to the next and by default it uses session) etc...

    So with MVC the approach used to generate the HTML page is changing but all the rest should be the same...

    Edit: you stick with ASP.NET 4.x or you are moving also to ASP.NET Core. In this later one you have also https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio

    Wednesday, May 19, 2021 7:42 AM
  • User1686398519 posted

    Hi smitapinky2021, 

    Is session variable  not used in MVC.

    If it is an ASP.NET MVC project, you can use Session like this:

    //Save to session state in the action method or view
    Session["test"] ="testdata";
    //Read from session state in the action method or view
    var test = Session["test"];

    If it is an ASP.NET Core MVC project, you can use Session like this:

    1. To enable the session middleware, Startup must contain:

      1. Any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session.
      2. A call to AddSession in ConfigureServices.
      3. A call to UseSession in Configure.
    2. After configuring the session state, you can set and get the session value as follows:
      • // Requires: using Microsoft.AspNetCore.Http;
        if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
        {
            HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        }
        var name = HttpContext.Session.GetString(SessionKeyName);
    3. Session state

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2021 8:13 AM
  • User-791721227 posted

    Thanks Patricesc

    Wednesday, May 19, 2021 9:08 AM
  • User-791721227 posted

    Thanks YihuiSun

    Wednesday, May 19, 2021 9:08 AM