使用mvc.net中的basecontroler写入标题页 [英] Writing to your header page using basecontroler in mvc.net

查看:104
本文介绍了使用mvc.net中的basecontroler写入标题页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



快速问题,在asp.net webforms中,你扩展你的system.web.ui.page我的意思是

Hi Guys,

Quick question, in asp.net webforms, you extend your system.web.ui.page i mean

public class MyPage : Page
   {}



您可以使用


and you can write to your page header using

 protected override void OnPreRender(EventArgs e)
        {
  var jqueryitems = new Dictionary<string, string>
                                  {
                                      {"6", "jquery.common.js"},
                                      {"7", "jquery.autocomplete.js"},
                                         {"8", "jquery.autocomplete.common.js"},
                                      {"9", "json2.js"}
                                   
                                  };

            foreach (KeyValuePair<string, string> pair in jqueryitems)
            {
                var commonJquery = new HtmlGenericControl("script");
                commonJquery.Attributes.Add("type", "text/javascript");
                commonJquery.Attributes.Add("language", "javascript");
                commonJquery.Attributes.Add("src", string.Format("/common/home/client/jQuery/{0}", pair.Value));
                this.Header.Controls.AddAt(int.Parse(pair.Key), commonJquery);
            }
}





在mvc中你会怎么做同样的技术,但这次使用BaseControler或者是控制器类?

Paige



In mvc how would you go about doing the same technique,but this time using BaseControler or controler class?
Paige

推荐答案

我现在能够想象的最简单方法是使用部分的组合 ViewBag 。您可以使用这些部分(请参阅: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2.aspx [ ^ ])制作标题布局中的部分。您可以在执行操作期间向ViewBag添加类似于您提到的字典(或类似内容)以存储(收集)脚本引用需求。并且您可以使用该对象中收集的数据在视图中将必要的脚本引用呈现到标题部分。

您可以创建一个帮助类来使相关任务整齐有序。
The simplest way I can imagine right now is to use a combination of sections and ViewBag. You can use the sections (see: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3razor_topic2.aspx[^]) to make a header section in the layout. Than you can add a dictionary like you mentioned (or something similar) to ViewBag to store (gather) script reference needs during execution of the actions. And you can use the data gathered in that object to render in the view the necessary script references into the header section.
You can make a helper class to have related tasks organized tidy.


这篇关于使用mvc.net中的basecontroler写入标题页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆