RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

Included Components receive null ViewBag

Open HamiltonManalo opened this issue 7 years ago • 11 comments

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

HamiltonManalo avatar Sep 10 '19 10:09 HamiltonManalo

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. image 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.

HamiltonManalo avatar Sep 10 '19 14:09 HamiltonManalo

Did you find a solution for this issue?

weyert avatar Oct 15 '19 01:10 weyert

@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 avatar Oct 15 '19 08:10 HamiltonManalo

@HamiltonManalo Any chance you can create a failing test for me to repro the issue and fix it?

jzabroski avatar Nov 22 '19 16:11 jzabroski

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?

HamiltonManalo avatar Nov 22 '19 18:11 HamiltonManalo

You can put a PR to RazorLight. Failing tests are perfectly fine.

jzabroski avatar Nov 22 '19 19:11 jzabroski

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 avatar Nov 23 '19 01:11 HamiltonManalo

@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 avatar Dec 17 '19 23:12 jzabroski

@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.

HamiltonManalo avatar Dec 18 '19 10:12 HamiltonManalo

Tested this in my work repo and viewbag is no longer null. I will submit some tests shortly.

HamiltonManalo avatar Dec 20 '19 12:12 HamiltonManalo

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!!

jzabroski avatar Dec 20 '19 13:12 jzabroski