This is a migrated thread and some comments may be shown as answers.

Grid not binding a Checkbox to Boolean in Model

3 Answers 1038 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 22 Jan 2013, 03:40 PM
I've created the following editable grid bound to a model. The Date and Integer EditorTemplates work just fine.

However, our user case requires that we multi-select via a checkbox, which is why I added a Selected boolean in the Model. I'd rather cycle through these via the grid's data than by iterating through [tr]s, as it is a much cleaner way to do so.

Via a custom Checkbox EditorTemplate and the ClientTemplate I'm able to successfully display a checkbox. However, I have tried many ways to bind it to the actual Model property and have failed each time. I've tried @HTML.CheckboxFor and @HTML.Checkbox within my EditorTemplate, the latter working properly but not binding to the Model. The CheckboxFor throws a message about not being able to bind a null value... I've tried bool?, bool and System.Boolean, but all three would not bind properly.

View
@(Html.Kendo().Grid(Model.ExtensionDetails)
          .Name("ExtensionInfoGrid")
          .Columns(columns =>
                       {
                           columns.Bound(o => o.Selected)
                                .Title("")
                                .Width(50)
                                .ClientTemplate("<input type='checkbox' id='isSelected' name='isSelected' #if(Selected){#checked#}# value='#=Selected#' />")
                                .EditorTemplateName("Checkbox");
                           columns.Bound(o => o.ExtendFromDate)
                                .Title("Extend From Date *")
                                .Width(150)
                                .Format("{0:M/d/yyyy}")
                                .Filterable(false)
                                .Sortable(true)
                                .EditorTemplateName("Date");
                           columns.Bound(o => o.Units)
                                .Title("Units *")
                                .Width(100)
                                .EditorTemplateName("Integer");
                           columns.Bound(o => o.UnitType).Title("Unit Type");
                       })
          .Editable(editable => editable.Mode(GridEditMode.InCell))
          .DataSource(dataSource => dataSource
                                .Ajax()
                                .ServerOperation(false)
                                .Model(model =>
                                            {
                                                model.Id(m => m.Code);
                                                model.Field(m => m.Selected).Editable(true);
                                                model.Field(m => m.ExtendFromDate).Editable(true).DefaultValue(DateTime.Now);
                                                model.Field(m => m.Units).Editable(true);
                                                model.Field(m => m.UnitType).Editable(false);
                                            })
          )
)}
Model

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ProviderWebAppMVC.UmServiceRef;
 
namespace ProviderWebAppMVC.Models
{
    public class ExtensionDetail
    {
        public ExtensionDetail()
        {
            Selected = true;
            Code = string.Empty;
            Description = string.Empty;
            ExtendFromDate = DateTime.Now;
            ExtendToDate = DateTime.Now;
            Units = 0;
            UnitType = string.Empty;
        }
 
        public bool Selected;
        public string Code;
        public string Description;
        public DateTime ExtendFromDate;
        public DateTime ExtendToDate;
        public int Units { get; set; }
        public string UnitType { get; set; }
    }
}

3 Answers, 1 is accepted

Sort by
0
Sean
Top achievements
Rank 1
answered on 22 Jan 2013, 03:52 PM
When defining the Checkbox EditTemplate as:
@model bool
@(Html.CheckBoxFor(m => m)

I receive the following exception upon rendering:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Boolean'.
0
Sean
Top achievements
Rank 1
answered on 22 Jan 2013, 04:33 PM
I was actually able to solve this issue with the following code solution: Kendo Grid Bound Checkboxes.

Digging around these forums can be a bear! It'd be nice if we'd have the option to search by forum, instead of searching all forums.

0
Vladimir Iliev
Telerik team
answered on 24 Jan 2013, 08:52 AM
Hi Sean,

 
Thank for your feedback - we will consider implementing such functionality in the forum search. 

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Sean
Top achievements
Rank 1
Answers by
Sean
Top achievements
Rank 1
Vladimir Iliev
Telerik team
Share this question
or