RadioButtons in ClientTemplate using an enum

2 posts, 1 answers
  1. Michael
    Michael avatar
    6 posts
    Member since:
    Sep 2014

    Posted 15 May Link to this post

    I have a grid in which one of the columns I would like to display/edit using RadioButtons.  The underlying value is an enum.  I'm still working on getting the column to display the correct choice.  Right now, none of the radio buttons are checked.  What am I doing wrong?  Below please see the grid, and the enum declaration (in case that's an issue)

     

    @(Html.Kendo().Grid<HVMS.BusinessLayer.Models.Metric>()
        .Name("MetricGrid")
        .Columns(columns =>
        {
     
            columns.Bound(c => c.Name);
            columns.Bound(c => c.ThresholdAssignment).ClientTemplate(
                "<span>Unassigned</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 'Unassigned') { # checked = 'checked' # } # />" +
                "  <span>Weekly</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 'Weekly') { # checked = 'checked' # } # />" +
                "  <span>Monthly</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 'Monthly') { # checked = 'checked' # } # />"
                );
     
        })
        .ToolBar(toolbar =>
        {
            toolbar.Save();
            toolbar.Excel();
            toolbar.Pdf();
        })
        .Editable(editable => editable.Mode(GridEditMode.InCell))
        .Navigatable()
        .Scrollable(s => s.Height("auto"))
        .Selectable(selectable => selectable
              .Mode(GridSelectionMode.Single)
              .Type(GridSelectionType.Row))
        .AutoBind(false)
        .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .Model(model => model.Id(p => p.Id))
            .Read(read => read.Action("Metrics_Read", "MetricThreshold").Data("additionalData"))
            .Update(update => update.Action("Metrics_Update", "MetricThreshold"))
     
        )
          )

     

    public enum ThresholdAssignment { Unassigned, Weekly, Monthly }
  2. Answer
    Georgi
    Admin
    Georgi avatar
    122 posts

    Posted 16 May Link to this post

    Hi Michael,

    I examined your code and your the cause if the error is the if statements where you check for the value of the Enumeration. You are comparing them as strings but actually they are integers. If you check if their value is their representative number your logic works as expected.

    Please modify the code for the ClientTemplate as illustrated below and the error should disappear.


    columns.Bound(c => c.ThresholdAssignment).ClientTemplate(
                "<span>Unassigned</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 0) { # checked = 'checked' # } # />" +
                "  <span>Weekly</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 1) { # checked = 'checked' # } # />" +
                "  <span>Monthly</span> <input type='radio' name='Metrics[#= index(data)#].ThresholdAssignment'  # if (ThresholdAssignment == 2) { # checked = 'checked' # } # />"
                );


     You can ensure that they have the right values by explicitly assigning them.


    public enum ThresholdAssignment
    {
       Unassigned = 0,
       Weekly = 1,
       Monthly = 2
    }




    Regards,
    Georgi
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top