Skip to content

Instantly share code, notes, and snippets.

@Logrythmik
Created July 19, 2012 22:26
Show Gist options
  • Save Logrythmik/3147296 to your computer and use it in GitHub Desktop.
Save Logrythmik/3147296 to your computer and use it in GitHub Desktop.
Razor Script Block Templated Delegate
private const string SCRIPTBLOCK_BUILDER = "ScriptBlockBuilder";
public static MvcHtmlString ScriptBlock(this WebViewPage webPage,
Func<dynamic, HelperResult> template)
{
if (!webPage.IsAjax)
{
var scriptBuilder = webPage.Context.Items[SCRIPTBLOCK_BUILDER]
as StringBuilder ?? new StringBuilder();
scriptBuilder.Append(template(null).ToHtmlString());
webPage.Context.Items[SCRIPTBLOCK_BUILDER] = scriptBuilder;
return new MvcHtmlString(string.Empty);
}
return new MvcHtmlString(template(null).ToHtmlString());
}
public static MvcHtmlString WriteScriptBlocks(this WebViewPage webPage)
{
var scriptBuilder = webPage.Context.Items[SCRIPTBLOCK_BUILDER]
as StringBuilder ?? new StringBuilder();
return new MvcHtmlString(scriptBuilder.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment