这是我的静态课程:
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指令或程序集引用?
我从这个答案得到了这个帮助方法:
pass enum to html.radiobuttonfor MVC3
最佳答案
InputExtensions.RadioButtonFor
扩展方法位于System.Web.Mvc.Html
命名空间中,因此需要为此命名空间添加using
子句