为什么我找不到 RadioButtonFor 方法?
这是我的静态类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Text;
using System.Web;
namespace Foo.WebUI.Infrastructure
{
public static class HtmlHelpers
{
public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", altText);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var names = Enum.GetNames(metaData.ModelType);
var sb = new StringBuilder();
foreach (var name in names)
{
var id = string.Format(
"{0}_{1}_{2}",
htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
metaData.PropertyName,
name
);
//---------------------------------------ERROR HERE!-----------------------------
var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
sb.AppendFormat(
"<label for=\"{0}\">{1}</label> {2}",
id,
HttpUtility.HtmlEncode(name),
radio
);
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
当我编译它时,我收到此错误:
“System.Web.Mvc.HtmlHelper”不包含以下定义 'RadioButtonFor' 且没有扩展方法 'RadioButtonFor' 接受 “System.Web.Mvc.HtmlHelper”类型的第一个参数可以是 找到(您是否缺少 using 指令或程序集引用?
我从这个答案中得到了这个辅助方法:
Here is my static class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Text;
using System.Web;
namespace Foo.WebUI.Infrastructure
{
public static class HtmlHelpers
{
public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", altText);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var names = Enum.GetNames(metaData.ModelType);
var sb = new StringBuilder();
foreach (var name in names)
{
var id = string.Format(
"{0}_{1}_{2}",
htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
metaData.PropertyName,
name
);
//---------------------------------------ERROR HERE!-----------------------------
var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
sb.AppendFormat(
"<label for=\"{0}\">{1}</label> {2}",
id,
HttpUtility.HtmlEncode(name),
radio
);
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
When I compile this, I get this error:
'System.Web.Mvc.HtmlHelper' does not contain a definition for
'RadioButtonFor' and no extension method 'RadioButtonFor' accepting a
first argument of type 'System.Web.Mvc.HtmlHelper' could be
found (are you missing a using directive or an assembly reference?
I got this helper method from this answer:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
InputExtensions.RadioButtonFor
扩展方法位于System 中.Web.Mvc.Html
命名空间,因此需要为此命名空间添加using
子句The
InputExtensions.RadioButtonFor
extension method is in theSystem.Web.Mvc.Html
namespace, so you need to add ausing
clause for this namespace将
using System.Web.Mvc.Html;
添加到代码顶部add
using System.Web.Mvc.Html;
to the top of your codeRadioButtonForEnum
方法不是HtmlHelper
类的一部分,它是一个扩展。您必须包含扩展类所在的命名空间:您可以在 该方法的文档页面。
The
RadioButtonForEnum
method is not a part of theHtmlHelper
class, it's an extension. You have to include the namespace where the extension class is:You can see the namespace to use on the documentation page for the method.
为了使其始终可访问,您可以在
Views\Web.config
文件中添加对 HtmlHelper 命名空间的引用,如下所示,以便它始终在您的视图中可见,而不必每次都包含它时间。To make this always accessible, you can add a reference to your HtmlHelper namspace in your
Views\Web.config
file as below, so that it will always be visible from your views, without having to include it every time.