Why can't I find the RadioButtonFor method?

2019-06-17 03:42发布

问题:

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

回答1:

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



回答2:

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



回答3:

The RadioButtonForEnum method is not a part of the HtmlHelper class, it's an extension. You have to include the namespace where the extension class is:

using System.Web.Mvc.Html;

You can see the namespace to use on the documentation page for the method.



回答4:

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.

 <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>