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

Replacement for DefaultDataItem

5 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
carlg
Top achievements
Rank 1
carlg asked on 10 Sep 2012, 02:42 PM
The MVC extensions grid allowed me to specify a default data item by using .DefaultDataItem

.Editable(editing => editing.Mode(Kendo.Mvc.UI.GridEditMode.InCell).DefaultDataItem(Model.defaultPerson))

How is this accomplished in Kendo?

5 Answers, 1 is accepted

Sort by
0
carlg
Top achievements
Rank 1
answered on 13 Sep 2012, 02:47 PM
If anyone is following this thread.  It is my understanding that Telerik did not build this functionality into Kendo.

Not sure why they would take away functionality, but it seems as if they did.
0
Rosen
Telerik team
answered on 14 Sep 2012, 07:18 AM
Hello carlg,

Although, the DefaultDataItem has been removed the default values can be set through DataSource Model definition. Please refer to this help article for more details.

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Arun
Top achievements
Rank 1
answered on 26 Sep 2012, 04:53 PM
"this help article" link does not work. could you show steps to set default value?
0
carlg
Top achievements
Rank 1
answered on 26 Sep 2012, 05:22 PM
Arun,

Just set your default values like this:

.DataSource(dataSource => dataSource.Ajax()
    .Batch(true)
    .ServerOperation(false)
.Model(model =>
    {
            model.Id(p => p.Id);
      model.Field(c => c.name).DefaultValue(Model.defaultObject.Name);
    })
0
Dimitrij
Top achievements
Rank 1
answered on 09 Jun 2015, 06:00 AM

It's a bummer that there's no adequate replacement for DefaultDataItem(), since I don't want to set the DefaultValue() for every field manually.

For everybody who's interested, I wrote an extension method that accomplishes the core functionality of DefaultDataItem(). It reads every property of a default item and sets the Field() and DefaultValue() in the data source model definition:

/// <summary>
/// Extensions of the DataSourceModelDescriptorFactory class.
/// </summary>
public static class DataSourceModelDescriptorFactoryExtensions
{
    /// <summary>
    /// Sets a default data item.
    /// </summary>
    /// <typeparam name="TModel">The model entity type.</typeparam>
    /// <param name="dataSourceModelBuilder">The data source model builder.</param>
    /// <param name="defaultDataItem">The default data item that should be used.</param>
    /// <returns>The extended data source model builder.</returns>
    public static DataSourceModelDescriptorFactory<TModel> DefaultDataItem<TModel>(
        this DataSourceModelDescriptorFactory<TModel> dataSourceModelBuilder,
        TModel defaultDataItem) where TModel : class
    {
        // for every property of the entity => set the corresponding field default value
        var propertyInfos = typeof(TModel).GetProperties();
        foreach (var propertyInfo in propertyInfos)
        {
            dataSourceModelBuilder.Field(propertyInfo.Name, propertyInfo.PropertyType).DefaultValue(propertyInfo.GetValue(defaultDataItem));
        }
  
        return dataSourceModelBuilder;
    }
}

Use it like this:

@(Html.Kendo().Grid<MyEntity>()
  ...
  .DataSource(ds => ds
    ...
    .Model(model =>
    {
      model.Id(n => n.Id);
      model.DefaultDataItem(myDefaultEntity);
    }
  )
)

HIH,
Dimitrij

Tags
Grid
Asked by
carlg
Top achievements
Rank 1
Answers by
carlg
Top achievements
Rank 1
Rosen
Telerik team
Arun
Top achievements
Rank 1
Dimitrij
Top achievements
Rank 1
Share this question
or