Search code examples
c#asp.net-mvckendo-uikendo-gridkendo-asp.net-mvc

Auto incrementing column in Kendo UI Grid with ASP.NET MVC


Is it possible to create a Kendo Grid that increments the Id column by 1 each time a new record is added? For example, I have a simple model class with 3 properties: Id, Title, Salary:

public class JobViewModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public double Salary { get; set; }
}

The razor code for the Grid looks like this:

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.JobViewModel>()    
    .Name("JobListing")    
    .Columns(columns => {        
        columns.Bound(x => x.Id);
        columns.Bound(x => x.Title);
        columns.Bound(x => x.Salary);
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .DataSource(dataSource => dataSource        
        .Ajax()         
        .Batch(true)
        .ServerOperation(false)
        .Model(model => model.Id(m => m.Id))
        .Create("Editing_Create", "Grid")
     )
)

Instead of the user typing the Id value, I wonder if there is a way to auto increment it.
So that when a new record is added, the "Editing_Create" Action Method receives a
JobViewModel object where the properties are set like this:

job.Id = 1 // next time this will be 2, and 3 ...
job.Title = "whatever the user typed"
job.Salary = "whatever the user typed"


Solution

  • @Scott Selby - Thanks. The first answer in this thread solves the problem.

    http://www.kendoui.com/forums/kendo-ui-complete-for-asp-net-mvc/grid/auto-increment-column.aspx