How to tell grid that a value can be null.

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

    Posted 28 Jan 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
    {
         [Column("ManpowerProjectTypeID")]
         public int ID { get; set; }

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

         [UIHint("ClientProjectType")]
         public ProjectTypeComboViewModel ParentProjectType { get; set; }
    }

    public class ProjectTypeComboViewModel
    {
         [Column("ManpowerProjectTypeID")]
         public int ID { get; set; }

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

    Then my grid looks like:

    @(Html.Kendo().Grid<ManpowerForecast.Web.ViewModels.ProjectTypeViewModel>()
    .Name("gridProjectType")
    .Columns(columns =>
    {
       columns.Bound(c => c.ProjTypeDesc).Width(200);
       columns.Bound(c => c.ParentProjectType).ClientTemplate("#=ParentProjectType.ProjTypeDesc#")
              .Width(150);
    })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .HtmlAttributes(new { style = "height: 550px" })
    .Refresh(true)
    .DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .ServerOperation(false)
    .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"))
    .PageSize(20)
    )
    )

     

    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
    20 posts
    Member since:
    Jun 2014

    Posted 28 Jan 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)
    .Name("ParentProjectType")
    .Filter(FilterType.Contains)
    .DataValueField("ID")
    .DataTextField("ProjTypeDesc")
    .AutoBind(true)
    .Placeholder("<Select Parent Project>")
    .BindTo((System.Collections.IEnumerable)ViewData["ProjectTypes"])
    .SelectedIndex(0)
    .Suggest(true)

     

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 29 Jan 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.



    Regards,
    Viktor Tachev
    Telerik
    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