-->

Radio Button doesn't reflect Model's value

2019-02-25 00:36发布

问题:

I have a kendo Grid for a class, and for that class I've built an editor template to produce a radio button for one of the fields.

This radio button doesn't reflect propertie's value and is always false, although I've checked the value, by printing it on the form, and I'm sure it's true. If I set a default value for that field, the radio button will reflect that value, regardless of the real value of the field.

I should note that I'm using a client template to display a text for that field, and it works fine.

This is the Grid:

    @(Html.Kendo().Grid<Class>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(x => x.BActive).ClientTemplate("#= BActive ? 'Open':'Close' #");
/// removed for brevity
        })
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .DataSource(ds => ds.Ajax()
            .Model(model => {
                model.Id(x => x.SWhCode);
                model.Field(x => x.BActive).DefaultValue(true);
            })
        )
    )

And this is the lines that produce the radio button inside the editorTemplate:

<label>
   @Html.RadioButtonFor(x => x.BActive, true, new { @class = "radio-inline" }) Open
</label>
<label>
   @Html.RadioButtonFor(x => x.BActive, false, new { @class = "radio-inline" }) Close
</label>

回答1:

bool type will be render in client like this

true => True
false => False

Have you try to inspect your element rendered in HTML? You'll see that the bool value transform into capitalize string. To overcome this you should change true and false in radio button with type string too.

Your view code should be like this

<label>
   @Html.RadioButtonFor(x => x.BActive, "true", new { @class = "radio-inline" }) Open
</label>
<label>
   @Html.RadioButtonFor(x => x.BActive, "false", new { @class = "radio-inline" }) Close
</label>