Home > Net >  Kendo RadioGroupFor setting HtmlAttributes base on boolean condition
Kendo RadioGroupFor setting HtmlAttributes base on boolean condition

Time:02-05

I am trying to use this object item.isRequired to set the HtmlAttributes as required but its not working. It work fine when I use the string value in the HtmlAttributes but not when I use the htmlAttributesData string.

var htmlAttributesData = item.isRequired ? "new { required = 'required', data_val_required = 'Question is Required' }" : "";

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

CodePudding user response:

The Html.Kendo() code wrapped in @() is processed on the server to generate javascript before the page is sent to the browser. When the server does this, htmlAttributesData does not exist because that is javascript code as far as I can tell. The server does not execute any javascript logic, it simply sends it to the browser.

Two options:

  1. Don't use the Html.Kendo server wrapper and instead use the query plugin syntax to declare the radio group with javascript. That will be processed on the browser where you can then use htmlAttributesData to set it up because it will exist at that point.

  2. Declare htmlAttributesData using server code. Maybe something like:

    .HtmlAttributes(@item.isRequired ? new { required = 'required', data_val_required = 'Question is Required' } : null)

  • Related