为什么我不能找到RadioButtonFor方法? [英] Why can't I find the RadioButtonFor method?

查看:134
本文介绍了为什么我不能找到RadioButtonFor方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的静态类:

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'不包含一个定义
  RadioButtonFor',没有扩展方法'RadioButtonFor接受
  类型'System.Web.Mvc.HtmlHelper'的第一个参数可能是
  (是否缺少using指令或程序集引用?

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

通过枚举html.radiobuttonfor MVC3

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/ee830410.aspx\"><$c$c>InputExtensions.RadioButtonFor扩展方法是在 System.Web.Mvc.Html 命名空间,所以你需要添加一个使用子句此命名空间

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

这篇关于为什么我不能找到RadioButtonFor方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆