Sep 20, 2013, 11:17 AM
Convert Razor view JavaScript markup to System.String
Hi.
I'm looking for an existing solution to the following problem.
I not found existing solution, how convert part Razor view html markup into string, like this:
I'm looking for an existing solution to the following problem.
@(Html.X().Button()
.Handler("Ext.Msg.alert('" + Model.Text + "');"))
This code is difficult into support and understanding (no JavaScript highlight and intellisence).I not found existing solution, how convert part Razor view html markup into string, like this:
@(Html.X().Button()
.Handler(Html.Script(@<script>Ext.Msg.alert('@Model.Text');</script>))
I wrote a helper to do that.public static class HtmlHelperExtensions
{
private static readonly Regex ScriptTagsRegEx = new Regex(@"^\s*<script.*?>|</script>\s*$", RegexOptions.Compiled);
public static string Script(this HtmlHelper source, Func<object, HelperResult> html)
{
return ScriptTagsRegEx.Replace(html(source.ViewContext).ToString(), string.Empty);
}
}
What do you think about this and whether there are better solutions?