【问题标题】:Why can't I find the RadioButtonFor method?为什么我找不到 RadioButtonFor 方法?
【发布时间】:2011-08-17 20:19:25
【问题描述】:

这是我的静态类:

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

【问题讨论】:

    标签: c# asp.net-mvc-3 using


    【解决方案1】:

    InputExtensions.RadioButtonFor扩展方法在System.Web.Mvc.Html命名空间中,所以需要为这个命名空间添加using子句

    【讨论】:

    • 这允许它编译。我想知道为什么通常的 Visual Studio 快捷方式 Ctrl + [DOT] 不起作用。谢谢。
    • @Sergio,此快捷方式不适用于扩展方法(但 ReSharper 等工具可以帮助您导入正确的命名空间)
    • @Sergio Ctrl-。不会搜索所有命名空间中的所有类以查看它们是否具有相关的扩展方法
    【解决方案2】:

    using System.Web.Mvc.Html; 添加到代码顶部

    【讨论】:

      【解决方案3】:

      RadioButtonForEnum 方法不是HtmlHelper 类的一部分,它是一个扩展。您必须包含扩展类所在的命名空间:

      using System.Web.Mvc.Html;
      

      您可以在documentation page for the method 上看到要使用的命名空间。

      【讨论】:

        【解决方案4】:

        要使其始终可访问,您可以在您的Views\Web.config 文件中添加对 HtmlHelper namspace 的引用,如下所示,以便它始终在您的视图中可见,而不必每次都包含它。

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

        【讨论】:

          猜你喜欢
          • 2010-10-11
          • 1970-01-01
          • 1970-01-01
          • 2013-08-30
          • 1970-01-01
          • 2021-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多