我需要将所有脚本放在页面底部,问题在于当我有局部视图时我无法使用“RenderSection”方法。发现如何添加的HtmlHelper扩展,它需要一个脚本文件的名称,一个很好的例子,加载到堆栈,然后又帮助将呈现出对基地布局: Razor section inclusions from partial viewMVC 4 - 如何将模板传递给html帮助器方法
这是伟大的 - 但我不想要为一小段脚本创建一个完整的JS文件,或者甚至是我想要放入的HTML文件。而且我不想将它全部作为字符串传递,我想要很好的格式化和智能感知,所以我想用一个模板,即:
@{Html.AddScript("test", @<text>
<script type="text/javascript">
function RefreshPreview() {
$('#AutoReplyHtml_Preview').html(
$('#htmlTemplate').html()
.replace('@@[email protected]@', $('#AutoReplySubject').val())
.replace('@@[email protected]@', $('#AutoReplyPreHeader').val())
.replace('@@[email protected]@', $('#AutoReplyHtml').val())
);
$('#AutoReplyPlainText_Preview').html(
$('#plainTextTemplate').html()
.replace('@@[email protected]@', $('#AutoReplyPlainText').val())
);
}
$(document).ready(function() {
RefreshPreview();
});
</script>
</text>);}
问题是 - 怎么我得到模板的值到我的方法,我有这样的代码符合,但不知道如何获取数据出来的“code”参数:
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()); //Doens't work!
return string.Empty;
}
如何调整委托参数以获取模板内的值?
完美!这正是我所需要的 - 当涉及到Func时,仍然是一个新手 - 只需要调整一下,就必须使其成为IHtmlString:public static IHtmlString AddBottomContent(this HtmlHelper htmlHelper,string title,Func
嗯,有趣。我在我的代码中使用'HelperResult'(实现'IHtmlString'),但我很高兴你提到它;也许'IHtmlString'更一般,我应该在我的代码中使用。 –