2011-07-04 8 views
0

mvc3でhtmlヘルパーを使用するときに例外が発生します。もともとLabelForとTextAreaForをクラスの 'virtual'パラメータで使用すると例外がありました。仮想キーワードを削除したところ、うまくいきました。'System.Action'は1つの引数を取らないHtmlHelper MVC3

LabelForとRadioBoxForEnum(カスタムヘルパー)を追加するようになりました。これらはenum(これは仮想ではありません)を使用しており、もう例外が発生しています。

違いがあるかどうかはわかりませんが、これをTelerikタブに入れました。

例外は次のとおりです。

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>); 
相続人

ヘルパーのコード(私は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()); 
    } 
} 

ありがとう:ここ

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

コードですあらかじめ。

J

+0

あなたはRadioBoxForEnumのコードを投稿してもらえますか? –

+0

@ steve-wilkes OK、ヘルパーコードを投稿に追加しました。 –

答えて

0

[OK]をので、私はこれを行うための別の方法を発見し、私が見つけた、これはすべての問題で動作する次のコードを使用して、エディタのテンプレートを作成したとして、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") 
関連する問題