简体   繁体   English

MVC 4-如何将模板传递给html helper方法

[英]MVC 4 - How to pass a template to an html helper method

I need to have all my scripts at the bottom of the page, problem is when I have a Partial View I cannot use the "RenderSection" approach. 我需要在页面底部放置所有脚本,问题是当我拥有部分视图时,我无法使用“ RenderSection”方法。 Found a great example of how to add a HtmlHelper extension which takes the name of a script file, loads into a stack, then another helper renders that out on the base layout: Razor section inclusions from partial view 找到了一个很好的示例,说明如何添加一个HtmlHelper扩展名,该扩展名带有脚本文件的名称,并加载到堆栈中,然后另一个助手将其呈现在基本布局上: Razor部分包含部分视图

That's great - but I don't want to have to create an entire JS file for a little chunk of script, or maybe even HTML, that I want to drop in. And I don't want to pass it all as a string, I want the nice formatting and intellisense, so I want to use a template ie: 太好了-但是我不想为要插入的一小段脚本甚至HTML创建一个完整的JS文件。我不想将其全部作为字符串传递,我想要好的格式化和智能感知,所以我想使用一个模板,即:

@{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 is - how to I get the value of the template into my method, I have this code which complies, but no clue how to get the data out of the "code" parameter: 问题是-如何将模板的值添加到我的方法中,我有符合要求的代码,但是不知道如何从“ 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;
}

How do I need to tweak the delegate parameter to get the value inside the template?? 我该如何调整委托参数以获取模板内部的值?

The helper architecture is designed so that it can accomodate scenarios where you are providing a template that will operate, for example, on each item in a list. 设计了帮助程序体系结构,以便它可以适应您提供将在例如列表中的每个项目上运行的模板的场景。 In such a scenario, you'd of course want to be able to pass it the "current" item when iterating through the list. 在这种情况下,您当然希望能够在遍历列表时将其传递给“当前”项目。

However, in other scenarios (such as yours), there is no current item . 但是,在其他情况下(例如您的情况), 没有当前项目 Yet, as you've discovered, you still have to declare a delegate as a parameter to your method that defines a method that consumes one parameter. 然而,正如您所发现的,您仍然必须将委托声明为方法的参数,该方法定义了使用一个参数的方法。 That's ok -- since you're not consuming that argument in your helper (you don't make use of the somewhat magical item parameter in your template) you can just pass it null in your implementation. 没关系-因为您没有在助手中使用该参数(您没有在模板中使用有些不可思议的item参数),所以可以在实现中将其传递为null Preferably, declare your parameter as a Func<object, IHtmlString> rather than a Func<object, object> , but regardless, just invoke code(null).ToString() to get the HTML-encoded string you need to render. 最好将参数声明为Func<object, IHtmlString>而不是Func<object, object> ,但是无论如何,只需调用code(null).ToString()即可获取需要呈现的HTML编码字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM