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:
pass enum to html.radiobuttonfor MVC3
The InputExtensions.RadioButtonFor
extension method is in the System.Web.Mvc.Html
namespace, so you need to add a using
clause for this namespace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With