Last post Mar 19, 2018 04:07 PM by vinod_praviram
Mar 07, 2018 05:20 PM|vinod_praviram|LINK
May be I am asking very basic question and there might be plenty of answers but still sometimes I find it difficult to make it work.
I have _Layout.cshtml, Index.cshtml and _Detail.cshtml, _Specification.cshtml
In layout I have declared sections "scripts" & "styles" which I can define in Index.cshtml but for partial views (_Detail.cshtml, _Specification.cshtml) I need specific css and js which need to be used only on those partial views as I can render those partial
views at any view in application so I would not want to write that specific css and js all those parent views.
So kindly tell me what is optimal and right way to write css and js specific to partial views be it rendered as ajax request or normal request.
If I try to write $(document).ready... statement in partial view then it logs error in browser console that $ is not defined even though _layout.cshtml has reference to jQuery library.
And can we use sections (scripts & styles) in partial views ?
Mar 07, 2018 07:23 PM|bruce (sqlwork.com)|LINK
as CSS is defined at the top, you can add css to the partial.
you can move the script and script section in layout to the top (with slight render performance cost). then the script can be inline.
you can also make a script helper that add the scripts to a collection in the view context, and second one in the layout that renders the scripts. maybe 20 lines of code.
@using Html.Deferred("<script> ....</script>")
note: if the partials are used more than once a page, you could add key.
Mar 08, 2018 04:46 PM|vinod_praviram|LINK
Hi, can you please elaborate your answer ?
Mar 18, 2018 10:43 AM|X.Daisy|LINK
Please remove the js code in head and remove '@RenderSection("scripts", required: false)'.
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
Mar 19, 2018 04:07 PM|vinod_praviram|LINK
Thanks for replying on it. But it is advised in general to keep JS at very last of the body tag so that page can be loaded faster.
Also there would be many views which have different JS utilities which I can not import on Layout page otherwise it will end up having a lot of JS reference and also irrelevant to many Views, if RenderScript section is removed
Kindly suggest some other work around.