1

I updated a Enum Editor I found online to generate kendo radio controls instead of regular but the first radio button is generated with correct attributes and the remaining are wrong and at runtime, the whole set of radio buttons are not clickable

Here is the .cshtml for the editor:

@model 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 >
            @(Html.Kendo().RadioButton()
                .Label(li.Text)
                .Name(prefix)
                .Value(li.Value)
                .Checked(li.Selected)
                .HtmlAttributes(new { @id = fieldName })
            )

            @*
            This works properly
            @Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName, @class = "k-radio" })
            @Html.Label(fieldName, li.Text, new { @class = "k-radio-label" })
            *@
        </div>
    }

    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

Here is the first radio on the form:

<div>
     <input name="StaffType" class="k-radio k-radio" id="StaffType_0" type="radio" checked="checked" value="DACStaff" data-val-required="The StaffTypeEnum field is required." data-val="true">
     <label class="k-radio-label" for="StaffType_0_DACStaff">DAC Staff</label>
</div>

And here is the next one:

<div>
     <input name="StaffType" class="k-radio k-radio" id="StaffType_1" type="radio" value="AirportStaff">
     <label class="k-radio-label" for="StaffType_1_AirportStaff">Airport Staff</label>
</div>

I see that the class tag k-radio is applied twice and except the first element has the data-* attributes but second radio button and onwards, the generated code is missing attributes.

Can someone point out why the generated code is not functioning?

Usage:

<div class="form-group">
    @Html.LabelFor(m => m.StaffType, new { @class = "col-sm-3 control-label" })
    <div class="col-sm-6">
        @Html.EditorFor(m => m.StaffType, "RadioButtonListEnum")
    </div>
</div>
ataravati
  • 8,891
  • 9
  • 57
  • 89
DoomerDGR8
  • 4,840
  • 6
  • 43
  • 91
  • Your question doesn't have anything to do with Kendo UI or even Kendo asp.net MVC. This is just an asp.net MVC question. Also, I don't understand why you're making this so complicated. This will probably help you: http://stackoverflow.com/questions/18542060/mvc4-enum-and-radio-button-list – ataravati May 09 '16 at 18:21
  • It is because I posted a working solution. If you comment out the Kendo wrapper and un-comment the regular one, it works. I need it to work with the Kendo wrapper. – DoomerDGR8 May 09 '16 at 18:22
  • Regardless of that, your code doesn't make much sense. You're doing a lot of stuff that shouldn't be done in the view. The SelectList should've been created in the controller, not in the view. Also, this could be the problem: http://stackoverflow.com/questions/30345143/radio-button-doesnt-reflect-models-value – ataravati May 09 '16 at 18:26

0 Answers0