How can I include css files from an MVC partial control?

后端 未结 9 1550
南笙
南笙 2020-12-28 09:15

I\'m using ASP.NET MVC and I have a partial control that needs a particular CSS & JS file included. Is there a way to make the parent page render the script

相关标签:
9条回答
  • 2020-12-28 09:42

    I am new to MVC... I had the challenge recently. I used MVC View Page instead of partial view, added its own master page (because I have a css which are shared across multiple views) and added css to master. Hope this info helps.

    0 讨论(0)
  • 2020-12-28 09:42

    Include a place holder in your master template:

    <head runat="server>
        <asp:ContentPlaceHolder ID="head" runat="server" />
    </head>
    

    and in your control

    <asp:Content ID="head" runat="server">
        <script src="something.js"></script>
    </asp:Content>
    
    0 讨论(0)
  • 2020-12-28 09:52

    If I have requirements for CSS/Javascript in a partial view, I simply make sure that any page that may include the partial view, either directly or as content retrieved from AJAX, has the CSS/Javascript included in it's headers. If the page has a master page, I add a content placeholder in the master page header and populate it in the child page. To get intellisense in the partial view, I add the CSS/Javascript includes in the partial view but wrapped with an if (false) code block so they are not included at runtime.

    <% if (false) { %>
       <link href=...
       <script type=...
    <% } %>
    
    0 讨论(0)
提交回复
热议问题