0

I have a viewcomponent where I do some queries and I need to store some informations that need to be used into my layout. In my layout, I am using a partial view on in this partial view I need to use the data stored into the ViewComponent. So my layout looks like:

<!DOCTYPE html>
<html>
<head>
    <partial name="_MyPartialView.cshtml" />
</head>
<body>
    @RenderBody()
</body>
</html>

So the first idea was to transfer the data with TempData but as it use the session the object we store look to big to be contained into the session so it cause some very weird issues.

So I have try to use the ViewData but apparently it stay empty in my partial view. If I trust this post: How share ViewData between ViewComponent in Asp.net core it is just used to transfer data from controller to view but probably not to the layout?

So finally I have tried to use httpContextAccessor.HttpContext.Items["xxx"] but it is empty when I try to get it back into the partial again.

Do I have an alternative solution?

Regards,

EDIT: Sorry I don't know why but now I am able to retreive the value of httpContextAccessor.HttpContext.Items in my partial but still I have a warning that say that injecting IHttpContextAccessor is a performance as it have an impact on the InvokeAsync so if you have a better option please tell me.

Kapoue
  • 847
  • 2
  • 11
  • 15

0 Answers0