I need to have all my scripts at the bottom of the page, the problem is when I have a partial view. I can not use the "RenderSection" approach. I found a great example of how to add the HtmlHelper extension, which takes the script file name, loads onto the stack, and then another helper agent displays this on the basic layout: Turning on razor sections from partial view
This is great - but I don’t want to create an entire JS file for a small piece of script, or maybe even for the HTML I want to go into. And I do not want to pass it all as a string, I want beautiful formatting and intellisense, so I want to use a template, i.e.
@{Html.AddScript("test", @<text> <script type="text/javascript"> function RefreshPreview() { $('#AutoReplyHtml_Preview').html( $('#htmlTemplate').html() .replace('@@ MESSAGE_TITLE@ @', $('#AutoReplySubject').val()) .replace('@@ PRE_HEADER@ @', $('#AutoReplyPreHeader').val()) .replace('@@ MESSAGE_BODY@ @', $('#AutoReplyHtml').val()) ); $('#AutoReplyPlainText_Preview').html( $('#plainTextTemplate').html() .replace('@@ MESSAGE_BODY@ @', $('#AutoReplyPlainText').val()) ); } $(document).ready(function() { RefreshPreview(); }); </script> </text>);}
Problem: how to get the template value in my method, I have this code that matches, but don’t know how to get the data from the "code" parameter:
public static string AddScript(this HtmlHelper htmlHelper, string title, Func<object, object> code) { var ctx = htmlHelper.ViewContext.HttpContext; Dictionary<string, string> scripts = ctx.Items["HtmlHelper.AddScript"] as Dictionary<string, string>; if (scripts == null) { scripts = new Dictionary<string, string>(); ctx.Items.Add("HtmlHelper.AddScript", scripts); } scripts.Add(title, code.ToString());
How do I configure the delegate parameter to get the value inside the template?