Is there a way to add CSS references to a page from a partial view, and have them render in the page's <head>
(as required by the HTML 4.01 spec)?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
The following would work only if javascript were enabled. it's a little helper that i use for exactly the scenario you mention:
usage:
works for me, tho as stated, only if javascript is enabled, thus limiting its usefulness a little.
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:
You could also use the Telerik open source controls for MVC and do something like :
in the head section and
in the script section at the botttom of your page.
And you can keep adding scripts on any view , or partial view and they should work.
If you don't want to use the component you can always inspire yourself from there and do something more custom.
Oh, with Telerik you also have options of combining and compressing the scripts.
If you're using MVC3 & Razor, the best way to add per-page items to your section is to: 1) Call RenderSection() from within your layout page 2) Declare a corresponding section within your child pages:
/Views/Shared/_Layout.cshtml:
/Views/Entries/Index.cshtml:
The resultant HTML page then includes a section that looks like this:
You can use a
HttpModule
to manipulate the response HTML and move any CSS/script references to the appropriate places. This isn't ideal, and I'm not sure of the performance implications, but it seems like the only way to resolve the issue without either (a) a javascript-based solution, or (b) working against MVC principles.Another approach, which defeats the principles of MVC is to use a ViewModel and respond to the Init-event of your page to set the desired css/javascript (ie myViewModel.Css.Add(".css") and in your head render the content of the css-collection on your viewmodel.
To do this you create a base viewmodel class that all your other models inherits from, ala
In your master-page you set it to use this viewmodel
and your head-section you can write out the value of the Css property
Now, in your partial view you need to have this code, which is kinda ugly in MVC