【问题标题】:'System.Action' does not take 1 arguments HtmlHelper MVC3'System.Action' 不接受 1 个参数 HtmlHelper MVC3
【发布时间】:2011-07-04 13:12:48
【问题描述】:

在 mvc3 中使用 html 帮助程序时出现异常。我最初在使用带有类的“虚拟”参数的 LabelFor 和 TextAreaFor 时遇到了异常。当我删除 virtual 关键字时,它工作正常。

我现在来添加一个 LabelFor 和一个 RadioBoxForEnum(自定义助手),它们使用一个枚举(它不是虚拟的),我又遇到了异常。

不确定这是否有任何区别,但我已将其放入 Telerik 选项卡中。

例外是:

CS1593: Delegate 'System.Action' does not take 1 arguments

代码如下:

items.Add().Text("Categorisation").Content(@<text>
        <div class="TabContent">
            <div class="5050SplitLeft">
                @Html.LabelFor(o => o.ChangeProposalHeader.ChangeProposal.ProgrammeCategory, "Programme:")
                @Html.TextAreaFor(o => o.ChangeProposalHeader.ChangeProposal.ProgrammeCategory
                <br />
                @Html.LabelFor(o => o.ChangeProposalHeader.ChangeProposal.InterfaceCategory, "Interface:")
                @Html.TextAreaFor(o => o.ChangeProposalHeader.ChangeProposal.InterfaceCategory)
                <br />
                @Html.LabelFor(o => o.ChangeProposalHeader.ChangeProposal.TechnicalReadinessCategory, "Technical Readiness Level:")
                @Html.TextAreaFor(o => o.ChangeProposalHeader.ChangeProposal.TechnicalReadinessCategory)
                <br />
                @Html.LabelFor(o => o.ChangeProposalHeader.ChangeProposal.DesignChangeRequirementCategory, "Design Change Requirement Category:")
                @Html.TextAreaFor(o => o.ChangeProposalHeader.ChangeProposal.DesignChangeRequirementCategory)
                <br />
                @Html.LabelFor(o => o.ChangeProposalHeader.ChangeProposal.FitType, "Fit Type:")
                @Html.RadioButtonForEnum(o => o.ChangeProposalHeader.ChangeProposal.FitType)
            </div>
            <div class="5050SplitRight">
            </div>
        </div></text>);

这是 Helper 的代码(我是从 SO 上的另一个线程得到的)

 public static class HtmlExtensions
{
    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
            );

            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());
    }
}

提前致谢。

J

【问题讨论】:

  • 你能发布 RadioBoxForEnum 的代码吗?
  • @steve-wilkes 好的,我已经在帖子中添加了帮助代码..

标签: asp.net-mvc-3 exception razor telerik html-helper


【解决方案1】:

好的,所以我认为 RadioBoxForEnum 代码一定有问题,因为我找到了一种不同的方法,并使用我找到的以下代码创建了一个编辑器模板,这适用于任何问题。

Enum_radioButtonList.cshtml

@model Enum

@{
 // Looks for a [Display(Name="Some Name")] or a [Display(Name="Some Name", ResourceType=typeof(ResourceFile)] Attribute on your enum
Func<Enum, string> getDescription = en =>
{
    Type type = en.GetType();
    System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

    if (memInfo != null && memInfo.Length > 0)
    {

        object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute),
                                                        false);

        if (attrs != null && attrs.Length > 0)
            return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetName();
    }

    return en.ToString();
};
var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select(e =>
new SelectListItem()
{
    Text = getDescription(e),
    Value = e.ToString(),
    Selected = e.Equals(Model)
});
string prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
int index = 0;
ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

foreach (var li in listItems)
{
    string fieldName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);
    <div class="editor-radio">
    @Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName }) 
    @Html.Label(fieldName, li.Text)    
    </div>
}
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

这被称为如下:

@Html.EditorFor(o => o.ChangeProposalHeader.ChangeProposal.FitType, "Enum_radioButtonList")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2015-01-20
    相关资源
    最近更新 更多