Included Components receive null ViewBag
Describe the bug A clear and concise description of what the bug is. When using @IncludeAsync(Key, Model) The ViewBag passed in during CompileRenderAsync is not passed to the component.
To Reproduce Steps to reproduce the behavior: Call CompileRenderAsync using a template which has an IncludeAsync that expects a ViewBag to be available
Expected behavior A clear and concise description of what you expected to happen. ViewBag content to be available across all Included Components when rendering a template
Information (please complete the following information):
- OS: windows 10
- Platform: .Net Core 2.2
- RazorLight version 2.0.0-beta1
- Visual Studio version: VS Community 16.2.4
Is it possible to get access to IncludeAsync and ViewBag without using a typed version of TemplatePage?
My template hierarchy is structured similar to this where my layout is configured by an object passed through the ViewBag. The ViewModel and ViewBag are used to configure the bodys components.
However outside of my "Layout" and "Body" templates, I do not seem to have access to the ViewBag. Is this a bug or a feature as they say.
Did you find a solution for this issue?
@weyert I did find a solution though not as elegant as I'd hoped. I pulled the required configuration out of the viewbag in the template body and passed it into the components as tuple {await IncludeAsync(Path/To/Component.cshtml, (Model, (Configuration)ViewBag.Configuration))} Which worked without major issues
@HamiltonManalo Any chance you can create a failing test for me to repro the issue and fix it?
Yeah, I will get it over the weekend. What would be the best way to give you the test? PR to RazorLight or another repo?
You can put a PR to RazorLight. Failing tests are perfectly fine.
I duplicated the template structure that gave me the error in a test and I was not able to reproduce the bug on the most current version of the repo. I loaded what I believe was the last published version and tried running the tests against that and they failed.
My guess is that there have been nearly 2 years of changes since the last release and the issue has been fixed. It seems the method CompileRenderAsync(string key, object model, Type modelType, [ExpandoBag ViewBag = null]) has been completely removed. After the next publish I'll update the project and see if the issue has been resolved, if it has I'll close the issue. Otherwise I'll figure out how to reproduce the bug.
@HamiltonManalo Let me know if we can close this. If you created a test, still worth submitting a PR so that it adds coverage for regression purposes.
@jzabroski I will make a PR for them soon. The tests were failing because of issue https://github.com/toddams/RazorLight/issues/287. One test from my use case involved calling IncludeAsync from a layout which isn't working right this moment.
Tested this in my work repo and viewbag is no longer null. I will submit some tests shortly.
Hey @HamiltonManalo - Super thankful for your efforts! Glad to hear the problem is resolved, and thanks a ton for submitting some tests! I LOVE TESTS!!