为什么我找不到 RadioButtonFor 方法?

发布于 2024-11-30 06:23:25 字数 2177 浏览 4 评论 0原文

这是我的静态类:

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 指令或程序集引用?

我从这个答案中得到了这个辅助方法:

将枚举传递给 MVC3 的 html.radiobutton

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹤舞 2024-12-07 06:23:25

InputExtensions.RadioButtonFor 扩展方法位于 System 中.Web.Mvc.Html 命名空间,因此需要为此命名空间添加 using 子句

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

心凉怎暖 2024-12-07 06:23:25

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

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

惟欲睡 2024-12-07 06:23:25

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

using System.Web.Mvc.Html;

您可以在 该方法的文档页面

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.

记忆里有你的影子 2024-12-07 06:23:25

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文