Search code examples
asp.net-mvckendo-uikendo-asp.net-mvc

Combine MVC Helper


Is there anyway I can combine this MVC helper the only different is the HtmlAttributes. I tried to do something like @item.isRequired?HtmlAttributes? but that didnt work.

 if (@item.isRequired)
 {
      @(Html.Kendo().RadioGroupFor(m => m.MeetingPollingQuestions)
      .Name(string.Format("PollingResponses[{0}].Value", idx))
      .HtmlAttributes(new { required = "required", data_val_required = "Question is 
      Required" })
        .Items(i=>
           {
              foreach (var option in @item.RadioButtonList)
                 i.Add().Label(option.Label).Value(option.Value);
              })
         .Value("Value")
       )
 }
 else
{
     @(Html.Kendo().RadioGroupFor(m => m.MeetingPollingQuestions)
     .Name(string.Format("PollingResponses[{0}].Value", idx))
     .Items(i=>
         {
         foreach (var option in @item.RadioButtonList)
             i.Add().Label(option.Label).Value(option.Value);
         })
      .Value("Value")
      )
}

Solution

  • Something like this would simply do the job

    @{
      object reqAttributes = null;
    
        if(item.isRequired) {
            reqAttributes = new { required = "required" , data_val_required = "Question is 
      Required"  }
        }
        else {
            reqAttributes = new {  }
        }
    }
    

    An then

    @(Html.Kendo().RadioGroupFor(m => m.MeetingPollingQuestions)
          .Name(string.Format("PollingResponses[{0}].Value", idx))
          .HtmlAttributes(reqAttributes )
            .Items(i=>
               {
                  foreach (var option in @item.RadioButtonList)
                     i.Add().Label(option.Label).Value(option.Value);
                  })
             .Value("Value")
           )