Kendo UI with default ASP.NET MVC3 settings when adding a row to the grid

I am converting an existing application from Telerik MVC extensions to a new KendoUI product. I use grid control. How to specify default values ​​for columns when adding a new row to the grid?

With the old Telerik MVC extensions, I did the following:

.Editable(editing=>editing.Mode(GridEditMode.InCell).DefaultDataItem(Model.defaultItem))

My model's DefaultItem was my default value for added rows. So how do I do this with Kendo?

+5
source share
3 answers

Yo yo yo mate,

You need to specify a default value for each of the fields using the dataSource model configuration

Here is an example that you can use;)

@(Html.Kendo()
.Grid<TestModel>()
.Name("SomeOtherGridName")
.DataSource(ds => ds.Ajax().Read("test", "test").Model(
    x => {
        x.Field(c => c.Val1).DefaultValue(5);
        x.Field(c => c.Val2).DefaultValue("cool!");
    }
 ))
.Columns(columns =>
{
    columns.Bound(c => c.Val1);
    columns.Bound(c => c.Val2);
})
)
+10
source

DefaultDataItem MVC. , - MVC .

0

I wrote an extension method that performs basic functions DefaultDataItem(). It reads each property of the element by default and sets Field()it DefaultValue()in the definition of the data source model:

public static class DataSourceModelDescriptorFactoryExtensions
{
    public static DataSourceModelDescriptorFactory<TModel> DefaultDataItem<TModel>(
        this DataSourceModelDescriptorFactory<TModel> dataSourceModelBuilder,
        TModel defaultDataItem) where TModel : class
    {
        var propertyInfos = typeof(TModel).GetProperties();
        foreach (var propertyInfo in propertyInfos)
        {
            dataSourceModelBuilder
                .Field(propertyInfo.Name, propertyInfo.PropertyType)
                .DefaultValue(propertyInfo.GetValue(defaultDataItem));
        }

        return dataSourceModelBuilder;
    }
}

Use it as follows:

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

All Articles