3

I want to refresh the _layout page for updating data in the layout section, when a action method returns a view(). Currently when I am returning the View(), layout page data are not updating.

3
  • "updating data in the layout section" How you load your data? Could you share your code ? Commented Dec 20, 2022 at 7:55
  • Loading the data from session. In the action method we updating the session values and returning the view(), But not affecting on the _layout page. Commented Dec 20, 2022 at 8:03
  • Are you sure about what layout page are you trying to update? Do you have another layout page in the discovery path used by the engine? Commented Dec 20, 2022 at 8:19

1 Answer 1

3

Below is a demo to use session to pass the string to appear in _layout page in every page , you can refer to it.

1.Register IHttpContextAccessor and session in Program.cs

builder.Services.AddSession(); builder.Services.AddHttpContextAccessor(); ... app.UseSession(); 

2.In the controller, set a session variable:

HttpContext.Session.SetString("Photo", "p1"); 

3.In _layout.cshtml, inject IHttpContextAccessor implementation and use it to get the HttpContext and Session object from that.

@using Microsoft.AspNetCore.Http @inject IHttpContextAccessor HttpContextAccessor ... @HttpContextAccessor.HttpContext.Session.GetString("Photo") 

result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.