【发布时间】: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