Last active
February 11, 2016 13:20
-
-
Save ludo6577/dc9a8cbb926a30144013 to your computer and use it in GitHub Desktop.
ASP.NET MVC: Add resources to Layout from View/Partial/HtmlHelper http://stackoverflow.com/a/8559777/2576706
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
//... | |
@Html.RenderResources("css") | |
</head> | |
<body> | |
//... | |
@Html.RenderResources("js") | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helper | |
{ | |
public static MvcHtmlString HelperMethod(this HtmlHelper<TModel> htmlHelper) | |
{ | |
htmlHelper.Resource("<link rel='stylesheet' href='/Content/bootstrap-multiselect.css' type='text/css'/>", "css"); | |
htmlHelper.Resource("<script type='text/javascript' src='/Scripts/bootstrap-multiselect.js'></script>", "js"); | |
//... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* modified from: http://stackoverflow.com/a/8559777/2576706 | |
*/ | |
public static class ResourcesHtmlHelper | |
{ | |
/* | |
* Called by Partial or View that need to add Scripts to the _layout | |
*/ | |
public static IHtmlString Resource(this HtmlHelper htmlHelper, Func<object, HelperResult> template, string type) | |
{ | |
if (htmlHelper.ViewContext.HttpContext.Items[type] == null) | |
htmlHelper.ViewContext.HttpContext.Items[type] = new List<Func<object, HelperResult>>(); | |
((List<Func<object, HelperResult>>)htmlHelper.ViewContext.HttpContext.Items[type]).Add(template); | |
return new HtmlString(String.Empty); | |
} | |
/* | |
* Called by Helper or view that need to add Scripts to the _layout | |
*/ | |
public static MvcHtmlString Resource(this HtmlHelper htmlHelper, string template, string type) | |
{ | |
Func<object, HelperResult> func = delegate(object o) | |
{ | |
return new HelperResult | |
( | |
writer => | |
{ | |
writer.Write(template); | |
} | |
); | |
}; | |
Resource(htmlHelper, func, type); | |
return MvcHtmlString.Empty; | |
} | |
/* | |
* Called by the Layout to render scripts from partial/view/htmlHelper, .... | |
*/ | |
public static IHtmlString RenderResources(this HtmlHelper htmlHelper, string type) | |
{ | |
if (htmlHelper.ViewContext.HttpContext.Items[type] != null) | |
{ | |
List<Func<object, HelperResult>> resources = (List<Func<object, HelperResult>>)htmlHelper.ViewContext.HttpContext.Items[type]; | |
foreach (var resource in resources) | |
{ | |
if (resource != null) htmlHelper.ViewContext.Writer.Write(resource(null) + Environment.NewLine); | |
} | |
} | |
return new HtmlString(String.Empty); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
@Html.Resource(@<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>, "js")` | |
@Html.Resource(@<link rel="stylesheet" href="@Url.Content("~/CSS/style.css")" />, "css")` | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what does this
htmlHelper.Resource("", "css");
htmlHelper.Resource("<script type='text/javascript' src='/Scripts/bootstrap-multiselect.js'></script>", "js");
do ?