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

How to tell grid that a value can be null.

2 Answers 877 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 29 Jan 2016, 01:26 AM

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 Answers, 1 is accepted

Sort by
0
Lee
Top achievements
Rank 1
answered on 29 Jan 2016, 03:32 AM

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)

 

0
Viktor Tachev
Telerik team
answered on 29 Jan 2016, 09:18 AM
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
Tags
Grid
Asked by
Lee
Top achievements
Rank 1
Answers by
Lee
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or