Add CSS references to page's <head> from a partial view

后端 未结 6 2399
感动是毒
感动是毒 2021-02-20 02:21

Is there a way to add CSS references to a page from a partial view, and have them render in the page\'s (as required by the HTML 4.01

6条回答
  •  死守一世寂寞
    2021-02-20 03:03

    You could have the partial view load in a javascript block that drops in the style to the head, but that would be silly considering that you probably want the javascript block in the head section for the same reason.

    I recently discovered something pretty cool though. You can serialize a partial view into a string and send it back to the client as part of a JSON object. This enables you to pass other parameters as well, along with the view.

    Returning a view as part of a JSON object

    You could grab a JSON object with JQuery and ajax and have it loaded with the partial view, and then another JSON property could be your style block. JQuery could check if you returned a style block, if so then drop it into the head section.

    Something like:

    $.ajax(
    {
         url: "your/action/method",
         data: { some: data },
         success: function(response)
         {
              $('#partialViewContainer).html(response.partialView);
              if (response.styleBlock != null)
                   $('head').append(response.styleBlock);
         }
    });
    

提交回复
热议问题