Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将MVC2帮助程序移动到MVC3 razor视图引擎_Asp.net Mvc_Asp.net Mvc 3_Html Helper_Razor - Fatal编程技术网

Asp.net mvc 将MVC2帮助程序移动到MVC3 razor视图引擎

Asp.net mvc 将MVC2帮助程序移动到MVC3 razor视图引擎,asp.net-mvc,asp.net-mvc-3,html-helper,razor,Asp.net Mvc,Asp.net Mvc 3,Html Helper,Razor,在我的MVC2站点中,我有一个html助手,用于为我的页面添加Java脚本。在我的母版页中,我有我想要包含的主要Java脚本,然后在aspx页面中,我包含特定于页面的Java脚本 例如,我的Site.Master有如下内容: .... <head> <%=html.renderScripts() %> </head> ... //core scripts for main page <%html.AddScript("/scripts/jque

在我的MVC2站点中,我有一个html助手,用于为我的页面添加Java脚本。在我的母版页中,我有我想要包含的主要Java脚本,然后在aspx页面中,我包含特定于页面的Java脚本

例如,我的Site.Master有如下内容:

....
<head>
    <%=html.renderScripts() %> 
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....
  ...
  ...
  <head>
  @{
      Html.AddJavaScript("/Scripts/jQuery.js");
      Html.AddJavaScript("/Scripts/myLib.js");
   }
   //Render scripts
   @html.RenderScripts()
  </head>
 ....
@{
    Layout = "~/Views/Shared/MasterLayout.cshtml";
    ViewBag.Title = "Child Page";
    Html.AddJavaScript("/Scripts/register.aspx.js");
 }
 ....
 <div>some html </div>
<html>
   ....
   <title>@ViewBag.Title</title>
   <head>
         @{
            //core scripts
            html.AddScript("/scripts/jquery.js"); 
            html.AddScript("/scripts/myLib.js");
            ....
           }

           @RenderSection("Scripts", required:true) 

           //Have to call render scripts here, but not sure about this?
           @Html.RenderScripts()
   </head>
....
</html>
。。。。
...
//主页的核心脚本
....
然后在childaspx页面中,我可能还想包含其他脚本

...
//the page specific script I want to use
<% html.AddScript("/scripts/register.aspx.js") %>
...
。。。
//我要使用的特定于页面的脚本
...
因此,当呈现整个页面时,sitemaster占位符函数RenderScripts会在头部收集并呈现所有javascript文件。这个很好用

现在有了MVC 3和razor视图引擎,它们的页面布局行为就不同了,因为现在没有呈现/包含我的页面级Java脚本。现在我看到的都是LayoutMaster的内容

如何使用MVC 3和razor视图引擎获得解决方案。(已重新写入帮助程序以返回HTMLString;-))

供参考:我的主布局如下所示:

....
<head>
    <%=html.renderScripts() %> 
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....
  ...
  ...
  <head>
  @{
      Html.AddJavaScript("/Scripts/jQuery.js");
      Html.AddJavaScript("/Scripts/myLib.js");
   }
   //Render scripts
   @html.RenderScripts()
  </head>
 ....
@{
    Layout = "~/Views/Shared/MasterLayout.cshtml";
    ViewBag.Title = "Child Page";
    Html.AddJavaScript("/Scripts/register.aspx.js");
 }
 ....
 <div>some html </div>
<html>
   ....
   <title>@ViewBag.Title</title>
   <head>
         @{
            //core scripts
            html.AddScript("/scripts/jquery.js"); 
            html.AddScript("/scripts/myLib.js");
            ....
           }

           @RenderSection("Scripts", required:true) 

           //Have to call render scripts here, but not sure about this?
           @Html.RenderScripts()
   </head>
....
</html>
。。。
...
@{
AddJavaScript(“/Scripts/jQuery.js”);
AddJavaScript(“/Scripts/myLib.js”);
}
//渲染脚本
@html.RenderScripts()
....
子页面如下所示:

....
<head>
    <%=html.renderScripts() %> 
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....
  ...
  ...
  <head>
  @{
      Html.AddJavaScript("/Scripts/jQuery.js");
      Html.AddJavaScript("/Scripts/myLib.js");
   }
   //Render scripts
   @html.RenderScripts()
  </head>
 ....
@{
    Layout = "~/Views/Shared/MasterLayout.cshtml";
    ViewBag.Title = "Child Page";
    Html.AddJavaScript("/Scripts/register.aspx.js");
 }
 ....
 <div>some html </div>
<html>
   ....
   <title>@ViewBag.Title</title>
   <head>
         @{
            //core scripts
            html.AddScript("/scripts/jquery.js"); 
            html.AddScript("/scripts/myLib.js");
            ....
           }

           @RenderSection("Scripts", required:true) 

           //Have to call render scripts here, but not sure about this?
           @Html.RenderScripts()
   </head>
....
</html>
@{
Layout=“~/Views/Shared/MasterLayout.cshtml”;
ViewBag.Title=“子页面”;
AddJavaScript(“/Scripts/register.aspx.js”);
}
....
一些html
谢谢你的帮助

编辑=如果此问题不够清楚,仅用于解释

在生成“页面”时,我使用html.addJavascript(“filename.js”)收集设计师想要使用的所有javascript文件,并将其存储在字典中-(1)停止人们添加重复的js文件-最后,当页面准备好呈现时,我在标题中整齐地写出所有javascript文件。(2) -此帮助器有助于将JS保持在一个位置,并防止设计师到处添加javascript文件。这在MVC2中可以很好地处理母版/站点母版页。但我怎样才能用剃须刀做到这一点呢

编辑:html帮助程序代码:

....
private const string SCRIPTLINK = 
        "<script src=\"{0}\" type=\"text/javascript\"></script>";

public static HtmlString RenderScripts(this HtmlHelper helper)  {

    OrderedDictionary scripts = 
        helper.ViewContext.HttpContext.Items["JavaScripts"] as OrderedDictionary;

    StringBuilder sb = new StringBuilder(500);
    foreach (DictionaryEntry script in scripts)  {
                sb.AppendFormat(SCRIPTLINK, script.Key);
        }
     return new HtmlString(sb.ToString());
 }
....
。。。。
私有常量字符串脚本链接=
"";
公共静态HtmlString呈现脚本(此HtmlHelper帮助程序){
OrderedDictionary脚本=
作为OrderedDictionary的helper.ViewContext.HttpContext.Items[“JavaScripts”];
StringBuilder sb=新StringBuilder(500);
foreach(字典在脚本中输入脚本){
sb.AppendFormat(SCRIPTLINK,script.Key);
}
返回新的HtmlString(sb.ToString());
}
....

如果看不到HtmlHelpers的代码,就很难帮助您使用当前的解决方案。然而,另一种(也是非常常见的)方法是使用Razor部分

在布局页面中,使用
@RenderSection()

然后,您可以使用与您的示例类似的语法样式

@section Scripts {
  @Html.AddScript("myscript.js")
}

您的代码可能正在为布局页面和视图使用单独的词典


请尝试将词典存储在
ViewBag
HttpContext中。项目

感谢Slaks和David提供的帮助和提示。我终于找到了一个似乎有效的解决方案,但有些事情我不喜欢。如果有更好的方法,请随时指出

这是我的孩子页面:

@{
    ViewBag.Title = "Child Page";
 }

 @section Scripts {

     @{Html.AddJavaScript("/Scripts/register.aspx.js");}

  }

 <div>some html </div>
我的主布局如下所示:

....
<head>
    <%=html.renderScripts() %> 
</head>
...
//core scripts for main page
<%html.AddScript("/scripts/jquery.js") %>
<%html.AddScript("/scripts/myLib.js") %>
....
  ...
  ...
  <head>
  @{
      Html.AddJavaScript("/Scripts/jQuery.js");
      Html.AddJavaScript("/Scripts/myLib.js");
   }
   //Render scripts
   @html.RenderScripts()
  </head>
 ....
@{
    Layout = "~/Views/Shared/MasterLayout.cshtml";
    ViewBag.Title = "Child Page";
    Html.AddJavaScript("/Scripts/register.aspx.js");
 }
 ....
 <div>some html </div>
<html>
   ....
   <title>@ViewBag.Title</title>
   <head>
         @{
            //core scripts
            html.AddScript("/scripts/jquery.js"); 
            html.AddScript("/scripts/myLib.js");
            ....
           }

           @RenderSection("Scripts", required:true) 

           //Have to call render scripts here, but not sure about this?
           @Html.RenderScripts()
   </head>
....
</html>

....
@视图包。标题
@{
//核心脚本
AddScript(“/scripts/jquery.js”);
AddScript(“/scripts/myLib.js”);
....
}
@RenderSection(“脚本”,必需:true)
//必须在此处调用渲染脚本,但对此不确定?
@Html.RenderScripts()
....

这确实会以正确的顺序和位置呈现我的脚本标记,但确实需要客户端在子页面中调用html帮助程序renderScripts。这没关系,因为我们可以创建模板来帮助实现这一点。有更好的主意吗?

你在哪里浏览脚本?字典放在哪里?你不应该使用字典。使用
列表
。列表不能保证集合中元素的顺序或唯一性。我刚刚看到您的编辑。请注意,这不会阻止添加重复的脚本声明Hanks David-是的,我看到了。无论如何谢谢你的帮助,它仍然是有用的Hi Slaks,脚本文件存储在字典中,在静态帮助类中不要这样做。如果你一次收到多个请求,它将失败得可怕。好主意,amd使用项目上下文。谢谢,关于如何构造razor布局有什么建议吗?只要字典的范围正确,结构应该不重要。