Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

ViewComponent ViewData #8580

Closed
Eilon opened this issue Oct 10, 2018 · 6 comments
Closed

ViewComponent ViewData #8580

Eilon opened this issue Oct 10, 2018 · 6 comments
Assignees

Comments

@Eilon
Copy link
Member

Eilon commented Oct 10, 2018

From @csutorasr on Wednesday, 10 October 2018 00:13:50

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.

Copied from original issue: dotnet/aspnetcore#3604

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @csutorasr.
@pranavkm, can you please look into this? Thanks!

@mkArtakMSFT
Copy link
Member

Not sure this one is related or not: #7675

@csutorasr
Copy link

Thank you for your kindness.

b62499e#diff-25d98c04d15f91552e80a30fdd460174R99

I think this line in this commit solved the problem. I have seen it in the Razor pages code, but not in the viewcomponent.

Lines that I found where data comes from:

I think if there is any more result executor all of them should be fixed.

@mkArtakMSFT
Copy link
Member

@pranavkm, can you please see whether it makes sense to extend the fix to the above mentioned areas? Thanks!

csutorasr added a commit to csutorasr/Mvc that referenced this issue Oct 13, 2018
@csutorasr
Copy link

I wrote a test that should not fail. You can find it here: csutorasr@f2e9fd9 .

The text is written as it should be, but the data is not set. I would not make a PR as there is no solution in the commit.

@pranavkm
Copy link
Contributor

This behavior is by design. Partials and ViewComponents get a separate copy of ViewData and changing this behavior would be a breaking change. There are a couple of suggestions on StackOverflow (e.g. https://stackoverflow.com/a/43829111) that suggest ways to pass data around between view components and other Razor files.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants