在Web.Config中添加HtmlHelper NameSpace不起作用

Owa*_*shi 3 asp.net asp.net-mvc html-helper visual-studio-2010 razor

问题1:

大家好我已经开始学习ASP.NET MVC,我已经做了一个简单的扩展方法,就像这样

namespace MvcTestz  //Project is also named as "MvcTestz"
{
  public static class SubmitButtonHelper //extension method.
  {
    public static string SubmitButton(this HtmlHelper helper,string buttonText)
    {
        return string.Format("<input type=\"submit\" value=\"{0}\">",buttonText);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我添加了Custom HtmlHelper的命名空间Web.Config,就像这样

  <namespaces>
    <!--other namespaces-->
    <add namespace="MvcTestz"/>
    <!--other namespaces-->
  </namespaces>
Run Code Online (Sandbox Code Playgroud)

所以我可以在剃刀View中使用intellisense,但它自定义Helper并没有出现在一个View中(Home/View/About.cshtml).

在此输入图像描述

所以在另一个视图中(Home/View/Index.cshtml)我添加了namespace by @using MvcTestz;statement.

问题2:

在WebApp执行主页(Home/View/Index.cshtml)显示输入按钮文本而不将其呈现为HTML.

在此输入图像描述 在关于页面(Home/View/About.cshtml)服务器生成错误.(点击放大) 在此输入图像描述

更新:

  1. Intellisense问题解决了,我不得不编辑View Directory.SOLVED中的Web.Config.
  2. HtmlString 如果我想渲染一个Html按钮,应该使用.SOLVED.

KMa*_*Man 9

问题1:

应该<system.web.webPages.razor>在web.config 中的节点中注册Razor名称空间:

 <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="MvcTestz"/>

      </namespaces>
    </pages>
  </system.web.webPages.razor>
Run Code Online (Sandbox Code Playgroud)

问题2:HtmlString在帮助程序中使用而不是字符串:

public static HtmlString SubmitButton(this HtmlHelper helper, string buttonText)
{
    return new HtmlString(string.Format("<input type=\"submit\" value=\"{0}\">", buttonText));
}
Run Code Online (Sandbox Code Playgroud)