locked
how to pass value from one view to another view by session RRS feed

  • Question

  • User-1634604574 posted

    how can i pass value from one view to another view by session

    Thursday, February 14, 2019 8:03 AM

All replies

  • User-2054057000 posted

    This is how you can use Session in ASP.NET Core.

    First enable session in startup.cs class:

    a. Add services.AddSession(); to ConfigureServices() method

    b. app.UseSession(); to Configure() method.

    Then in your controller you can set some value to session like:

    HttpContext.Session.SetString("MyValue", "ASP.NET CORE Session");

    Then session as a service in ConfigureServices() method:

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    Then in your View you can access the session value like:

    <div>
    Session value: @HttpContextAccessor.HttpContext.Session.GetString("MyValue")
    </div>

    More information: Sessions in ASP.NET Core

    Thanks & Regards

    Thursday, February 14, 2019 9:48 AM
  • User1520731567 posted

    Hi zhyanadil.it@gmail.com,

    how can i pass value from one view to another view by session

    You could refer to this demo,when you pass data between,the compiler need to jump to action,you could set session and call session directly.

      [HttpPost]
            public ActionResult Index1()
            {
              ....
                //persist data for next request
                Session["passData"] = 123;
    
                return RedirectToAction("Index2");
            }
      public ActionResult Index2()
            {
               .....
                return View();
            }

    Index2 view:

    @{
        ViewBag.Title = "Index2";
    }
    
    <div>@Session["passData"]</div> //show: 123,you could call session directly


    In addition,you could also use ViewData/ViewBag,TempData,Hidden fields.. to pass data,you could refer to the picture about their maintain data:

    Best Regards.

    Yuki Tao

    Friday, February 15, 2019 10:05 AM