I am unable to call any section that I have defined from a view component. Has anyone done this yet?
You can use it to render sections for a Layout that the view component's view declares. However, sections defined in partials and view components don't flow back to the rendering view or it's Layout.
I think you thought I meant that I have a section defined in the VC and then I was expecting it to render in the layout, but no, just the opposite. In my layout, I have a section like this:
@RenderSection("Scripts", false)
And in my VC, I am trying to call that section like so:
@section Scripts {
<script type="text/javascript">
alert('did I make it here??? NO');
</script>
}
That make more sense?
Does seem like you're trying to define a section in the ViewComponent (that's what @section
does) and render it in the layout (RenderSection
). Like I said earlier, this isn't supported.
Umm, no. Not sure if you've read the documentation or ever created a layout file, but to define a section, you use @RenderSection("Scripts", false)
and then when you want to use those sections in your views, you simply use @section Scripts { // put script here }
per the documentation:
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout
I'm doing that and it works fine in a view, but not in a view component. According to the documentation though, it's not supposed to work:
Sections defined in a view are available only in its immediate layout page. They cannot be referenced from partials, view components, or other parts of the view system.
Can anyone here explain how that makes sense? Tell me a valid use of an enterprise view component where you wouldn't need CSS/JS???
@clockwiseq I'm scratching my head over here on why the hell I would need a view component if I can't use CSS/JS lol. I guess my only logical choice is to move my grid component into a view so I can use the grid-specific jquery I was trying to inject in the view component
That's precisely my issue. I'm not sure why _"they"_ didn't allow the VC's to access sections defined in a layout. It seems possible in the pipeline, but is above my head. There would never be an instance where I would create some reusable piece of HTML that would not require some type of front-end scripting. It seems like this is a fairly common scenario and I'm wondering if I'm the only one that has a problem with this?
There's some discussion for this feature request here - https://github.com/aspnet/Mvc/issues/2910. The guidance was to use a script \ css management feature as described here - https://github.com/aspnet/Mvc/issues/2910#issuecomment-328585001.
That's EXACTLY what I'm experiencing and wish existed. I will look at the script/css management feature and see if that works for me.
Closing this issue because it isn't clear to me if there is an issue remaining. If you feel there's still an issue, please re-open this issue or log a new issue with complete details of the problem. Thanks!
How do I reopen my issue? This is a valid request as I have seen tons of devs request and comment on this feature for months now and both requests have been closed. Maybe no one is seeing the bigger picture here. If I wanted to create a reusable component for my application inside my web project (should not violate SoC), this feature is a must as something as simple as wanting to create a select2 component should be example enough. If I need to elaborate, I can.
Most helpful comment
Umm, no. Not sure if you've read the documentation or ever created a layout file, but to define a section, you use
@RenderSection("Scripts", false)
and then when you want to use those sections in your views, you simply use@section Scripts { // put script here }
per the documentation:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout
I'm doing that and it works fine in a view, but not in a view component. According to the documentation though, it's not supposed to work:
Can anyone here explain how that makes sense? Tell me a valid use of an enterprise view component where you wouldn't need CSS/JS???