Why can't I find the RadioButtonFor method?

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" that takes the first argument of the type "System.Web.Mvc.HtmlHelper" may be (do you miss the using directive or assembly references?

I got this helper method from this answer:

pass enum to html.radiobuttonfor MVC3

+7
source share
4 answers

The Extension method InputExtensions.RadioButtonFor is in the System.Web.Mvc.Html , so you need to add a using clause for this namespace

+15
source

add using System.Web.Mvc.Html; to the top of your code

0
source

The RadioButtonForEnum method is not part of the HtmlHelper class, it is an extension. You must include a namespace in which the extension class:

 using System.Web.Mvc.Html; 

You can see the namespace that will be used on the documentation page for the method .

0
source

To make this always accessible, you can add a link to your HtmlHelper namespace in your Views\Web.config file, as shown below, so that it is always visible from your views without having to include it every time.

  <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="My.HtmlHelper.Namespace" /> <!-- Added here --> </namespaces> </pages> </system.web.webPages.razor> 
0
source

All Articles