2011-07-04 4 views
0

mvc3에서 html 헬퍼를 사용할 때 예외가 발생합니다. 원래 LabelFor 및 TextAreaFor 클래스의 '가상'매개 변수를 사용할 때 예외가 발생했습니다. 내가 가상 키워드를 제거했을 때 잘 작동했습니다.'System.Action'은 1 개의 인수를 사용하지 않습니다. HtmlHelper MVC3

이제 LabelFor 및 RadioBoxForEnum (사용자 지정 도우미)을 추가했습니다. 이러한 것들은 가상이 아닌 열거 형을 사용하며 예외가 다시 발생합니다.

어떤 차이가 있는지 알 수는 없지만 이것을 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>); 
을 Heres

도우미에 대한 코드 (내가 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

내가이 일을 다른 방법을 발견하고 내가 찾은 다음 코드를 사용하여 편집기 템플릿을 생성하고이 문제와 함께 작동 같이 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") 
관련 문제