dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.28k stars 9.96k forks source link

ViewComponent ViewData #3604

Closed csutorasr closed 5 years ago

csutorasr commented 5 years ago

MVC and Razor Pages: https://github.com/aspnet/Mvc

I tried to create a ViewComponent and try to use ViewData from it.

The ViewComponents are using different ViewContext from the page that executed it. This way I can't reach or set ViewData, that can be later passed to layout of the page.

I managed to reach the data of the parent with an ugly solution, while I kept trying:

(Component as IViewContextAware)?.Contextualize(ViewContext);

This way the ViewComponent could reach data from the page that invoked it, but the other way of the data cannot be made, because of the new context.

You could ask why I need ViewData in ViewComponent.

I am creating a "lot" (I hope it will be a lot) of libraries to make it easier to create a CMS system with ASP.NET Core. The only reference in my code is the MVC and EF, and I try to keep it for later. This way any project using MVC needs some CMS functionality, that can be inserted in no time without affecting the structure of the original project (only some entities in EF).

I needed a way to show views if I get a contentId. I would like to use the higher abstraction levels of the framework as changing less frequently.

  1. Pages with rewrite middleware was my first solution but those can be reached with the original URL too.
  2. ViewComponent seemed the best solution, but I couldn't set the ViewData.

If there's any other options that with I can solve this problem please tell me.

Eilon commented 5 years ago

This issue was moved to aspnet/Mvc#8580