Search code examples
c#kendo-uikendo-asp.net-mvc

How to set up default value for Null Type in Kendo UI asp net MVC


I use Kendo UI ASP NET MVC grid and I want to make the default value in Model section for nullable type(for example string) as below

@(Html.Kendo().Grid(Model.Comments)
              .Name("...")
              .DefaultSettings()
              .Columns(columns =>
              {
                  columns.Bound(c => c.JobId).Width("12%");
                  columns.Bound(c => c.ChangeDate).Width("15%");
                  columns.Bound(c => c.ChangeUserName).Width("12%");
                  columns.Bound(c => c.ProjectRole).Width("12%");
                  columns.Bound(c => c.Text).Width("18%");
              })
              .AddActionsByPermissions(new[]
              {
                  ...
              })
              .Editable(editable => editable.Mode(GridEditMode.InLine))
              .Sortable()
              .Resizable(resize => resize.Columns(true))
              .Selectable()
              .Scrollable(src => src.Height("auto"))
              .Pageable(p => p.PageSizes(new[] { 10 }).PageSizes(false))
              .Events(e =>
              {
                  e.Change("projectCommentsModule.onRowSelect");
                  e.DataBinding("projectCommentsModule.onGridDataBinding");
              })
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .ServerOperation(false)
                  .Read("Commentss", "ProjectComments")
                  .Model(m =>
                  {
                      m.Id(d => d.Id);
                      m.Field(d => d.ChangeDate).Editable(false).DefaultValue(DateTime.Now);
                      m.Field(d => d.ChangeUserName).DefaultValue(Model.CurrentUserProjectRole).Editable(false);
                      m.Field(d => d.ProjectRole).Editable(false).DefaultValue(Model.CurrentUserName);
                      m.Field(d => d.ProjectNumber).Editable(false).DefaultValue(Model.ProjectNumber);
                      m.Field(d => d.JobId).DefaultValue(0);
                  })
                  .Sort(s => s.Add(c => c.ChangeDate).Descending())
                  .Create(create => create.Action("AddComment", "ProjectComments"))
                  .Update(create => create.Action("UpdateComment", "ProjectComments"))
              )
        )

But for nullable type it does not set, so when I save record, I get on server null value but need get for example "SomeName", how I can fix it?


Solution

  • Right now, I think this is a bit of a bug in Kendo UI. As of last July 2019, I found this on a blog post from Telerik:

    I am afraid that with our last official release we introduced a regression bug that affects applying default values with some fields (in this case string type of fields).

    They gave this as a workaround solution and I think you can use it here. Add an event of type BeforeEdit, like so:

    .Events(e=>e.BeforeEdit("onBeforeEdit"))
    <script>
        function onBeforeEdit(e) {
          if(e.model.isNew()){
            e.model.YourField = "SomeName";
          }
        }
    </script>
    

    You'd then obviously change the field name and value to what your need.