How to tell grid that a value can be null.

3 posts, 0 answers
  1. Lee
    Lee avatar
    31 posts
    Member since:
    Jun 2014

    Posted 28 Jan 2016 Link to this post

    I'm creating a grid for a model that has child relationship that is not required.  The grid displays when the child has a value but if the child is null then I get a message that a value can't be null.  For example.   If my model is:

    public class ProjectTypeViewModel
         public int ID { get; set; }

         [Display(Name = "Project Type")]
         public string ProjTypeDesc { get; set; }

         public ProjectTypeComboViewModel ParentProjectType { get; set; }

    public class ProjectTypeComboViewModel
         public int ID { get; set; }

          [Display(Name = "Project Type")]
          public string ProjTypeDesc { get; set; }

    Then my grid looks like:

    .Columns(columns =>
       columns.Bound(c => c.ProjTypeDesc).Width(200);
       columns.Bound(c => c.ParentProjectType).ClientTemplate("#=ParentProjectType.ProjTypeDesc#")
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .HtmlAttributes(new { style = "height: 550px" })
    .DataSource(dataSource => dataSource
    .Events(events => events.Error("error_handler"))
    .Model(model =>

         model.Id(prop => prop.ID);
         model.Field(b => b.ParentProjectType).DefaultValue(ViewData["defaultProjectTypes"] as ProjectTypeComboViewModel);
    .Create(create => create.Action("CreateManpowerProjectTypes", "Manpower"))
    .Read(read => read.Action("ReadManpowerProjectTypes", "Manpower"))
    .Update(edit => edit.Action("UpdateManpowerProjectTypes", "Manpower"))
    .Destroy(delete => delete.Action("DestroyManpowerProjectTypes", "Manpower"))


    this displays just fine as long as ParentProjectType has a value but if it is null I get an error in the browser consul that reads: ProjTypeDesc is null.  If I comment out all the lines that have to do with the ParentProjectType the grid will display the data but off course doesn't contain the parent project type field.  How do I indicate in the grid that the data isn't required or is nullable?



  2. Lee
    Lee avatar
    31 posts
    Member since:
    Jun 2014

    Posted 28 Jan 2016 in reply to Lee Link to this post

    I've figured out to put the following for the column:

    columns.Bound(c => c.ParentProjectType).ClientTemplate("#= (ParentProjectType == null) ? ' ' : ParentProjectType.ProjTypeDesc #")

    However I'm still having an issue that while in batch edit mode if I click on the cell for ParentProjectType and don't make a selection it tells me that ID can't be null.  I have to hit escape to get off the cell.  I think this might be something in my ComboBox EditorTemplate but I'm not sure.  Below is code for my ComboBox.

     @(Html.Kendo().ComboBoxFor(model => model.ID)
    .Placeholder("<Select Parent Project>")


  3. Viktor Tachev
    Viktor Tachev avatar
    2212 posts

    Posted 29 Jan 2016 Link to this post

    Hello Lee,

    Please examine the following code-library that illustrates how you can bind a dropdown editor in a grid to a nullable field.

    Viktor Tachev
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top