gpt4 book ai didi

c# - 为什么我找不到 RadioButtonFor 方法?

转载 作者:太空狗 更新时间:2023-10-29 20:00:24 25 4
gpt4 key购买 nike

这是我的静态类:

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

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

pass enum to html.radiobuttonfor MVC3

最佳答案

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

关于c# - 为什么我找不到 RadioButtonFor 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7098919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com