认识到问题在asp.net MVC 3剃须刀HTML辅助 [英] Problem recognizing html helpers in asp.net mvc 3 razor
问题描述
这是我的HTML帮助是什么样子:
This is what my Html helper looks like:
namespace WebApp.WebUI
{
public static class HtmlExtensions
{
public static MvcHtmlString GenerateCaptcha(this HtmlHelper helper, string theme)
{
string publicKey = ConfigurationManager.AppSettings["CaptchaKey_Public"];
string privateKey = ConfigurationManager.AppSettings["CaptchaKey_Private"];
var captchaControl = new Recaptcha.RecaptchaControl
{
ID = "recaptcha",
Theme = theme,
PublicKey = publicKey,
PrivateKey = privateKey
};
var htmlWriter = new HtmlTextWriter(new StringWriter());
captchaControl.RenderControl(htmlWriter);
return new MvcHtmlString(htmlWriter.InnerWriter.ToString());
}
}
}
我想在此视图中使用它:
I tried using it in this view:
@{
ViewBag.Title = "Register";
}
@model WebApp.WebUI.ViewModel.RegisterModel
@using (Html.BeginForm("Register", "Auth", FormMethod.Post, new { Id = "ERForm" }))
{
@Html.GenerateCaptcha("clean")
}
它给了我这个错误:
It gives me this error:
CS1061:'System.Web.Mvc.HtmlHelper< WebApp.WebUI.ViewModel.RegisterModel>'不包含'GenerateCaptcha的定义,并没有扩展方法'GenerateCaptcha接受式的第一个参数System.Web.Mvc.HtmlHelper< WebApp.WebUI.ViewModel.RegisterModel>'可以找到(是否缺少using指令或程序集引用?)
我是什么做错了。我的命名空间是正确的。它不会在智能感知 @Html
What am I doing wrong. My namespaces are correct. It does not show up in the intellisense for @Html
推荐答案
您可以添加:
@using WebApp.WebUI
在你的Razor视图的顶部。
on the top of your Razor view.
如果你想重用很多不同的意见之间的这种帮助,以避免每次添加使用子句,你可以将它添加到<命名空间>
的部分〜/查看/ web.config中
文件:
And if you want to reuse this helper among many different views to avoid adding the using clause everytime you could add it to the <namespaces>
section of the ~/Views/web.config
file:
<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="WebApp.WebUI" />
</namespaces>
</pages>
</system.web.webPages.razor>
这样做确保您重新编译和重新打开的Razor视图的智能感知有时间把它捡起来后。
After doing this make sure you recompile and reopen the Razor view for the Intellisense to have time to pick it up.
这篇关于认识到问题在asp.net MVC 3剃须刀HTML辅助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!