“系统.操作”不接受 1 个参数 HtmlHelper MVC3

发布于 2024-11-18 15:25:10 字数 3010 浏览 1 评论 0原文

在 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

I'm getting an exception when using html helpers in mvc3. I got the exception originally when using LabelFor and TextAreaFor with a 'virtual' parameter of a class. When I removed the virtual keyword it worked fine.

I've now come to add a LabelFor and an RadioBoxForEnum (Custom helper) and these use an enum (which isn't virtual) and I'm getting the exception again.

Not sure whether it makes any difference but I've put this into a Telerik Tab.

The exception is:

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

Here's the code:

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

Heres the code for the Helper (I got this from another thread on 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());
    }
}

Thanks in advance.

J

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

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

发布评论

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

评论(1

椵侞 2024-11-25 15:25:10

好的,所以我认为 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")

Ok So i think there must be a problem with the RadioBoxForEnum code, as I found a different way of doing this and created an editor template using the following code I found and this works with any issues.

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

And this gets called as follows:

@Html.EditorFor(o => o.ChangeProposalHeader.ChangeProposal.FitType, "Enum_radioButtonList")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文