We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Essential JS2

Syncfusion jQuery based widgets are no longer in active development. Switch to our pure JavaScript based next generation Essential JS 2 library.

Syncfusion Feedback

how to return data from server while performing CRUD operation if IsIdentity column is defined

Platform: jQuery |
Control: ejGrid

When enabling the IsIdentity property for Auto incremented column then we should return the values along with the response which is generated the value for IsIdentity column. Please refer to the following code example,

JS

var dataManager = ej.DataManager({ url: "/Home/DataSource", insertUrl: "/Home/Insert", updateUrl: "/Home/Update", DeleteUrl: "/Home/Delete", adaptor:"UrlAdaptor"});

      

        $("#Grid").ejGrid({

            dataSource: dataManager,

            allowPaging: true,

            editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },

            toolbarSettings:{showToolbar:true,toolbarItems:["add","edit","cancel","update","delete"]},

            columns: [

                    { field: "OrderID", isPrimaryKey: true, isIdentity:true, type:"number", headerText: "Order ID" },

                    { field: "EmployeeID", type:"number", headerText: 'Employee ID' },

                    { field: "Freight", type:"number", headerText: 'Freight', format: "{0:C}" },

                    { field: "CustomerID",type:"string",headerText: 'Ship City' },

                    

            ],

            

 

        });

 

MVC

@(Html.EJ().Grid<EditableOrder>("Grid")

                           .Datasource(ds => ds.URL(@Url.Action("DataSource")).UpdateURL("/Home/Update").InsertURL("/Home/Insert").RemoveURL("/Home/Delete")

                    .Adaptor(AdaptorType.UrlAdaptor))

                    .AllowPaging()

                    .Columns(col =>

                     {

 

                        col.Field(p => p.OrderID).IsPrimaryKey(true).IsIdentity(true).HeaderText("Order ID").Add();

                        col.Field(p => p.EmployeeID).HeaderText("Employee ID").Add();

                        col.Field(p => p.Freight).HeaderText("Freight").Format("{0:C2}").Add();

                        col.Field(p => p.CustomerID).HeaderText("Customer ID").TextAlign(TextAlign.Left).Add();

                     })

            )

 

 

Server Side

  public ActionResult Insert(EditableOrder value)

        {

            OrderRepository.Add(value);

            var data = OrderRepository.GetAllRecords();

            return Json(value, JsonRequestBehavior.AllowGet);

        }

 

Note: Data source will be handled the Auto incremented column. Otherwise we need to set the value for IsIdentity column like as follows.

public ActionResult Insert(EditableOrder value)

        {

            

            var data = OrderRepository.GetAllRecords().Last();

            value.OrderID = ++data.OrderID;

            OrderRepository.Add(value);

            return Json(value, JsonRequestBehavior.AllowGet);

        }

 

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile