Kendo UI Grid posting back already Inserted Rows again

拈花ヽ惹草 提交于 2019-12-02 01:21:07

Your primary key cannot be 0 or null. If you are using auto-incrementing values then you should invoke the "re-read" of your dataSource after post. Check the values and make sure your data values are >0. Note: I have set the default value of the PK in the model to -1 in the column model to help with this.

This will happen if you don't return the "DGYR_PK" value of the newly inserted model. The data source needs a model instance to have its "id" field set in order not to consider it "new". When you return the "ID" as a response of the "create" operation the data source will work properly.

You can check this example for fully working Web API CRUD: https://github.com/telerik/kendo-examples-asp-net/tree/master/grid-webapi-crud

Here is the relevant code:

public HttpResponseMessage Post(Product product)
{
    if (ModelState.IsValid)
    {
        db.Products.AddObject(product);
        db.SaveChanges();

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.ProductID }));
        return response;
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}

After you have inserted a record, you need to return the id of that row, otherwise grid consider the previous row as a new row too.

In your create function you call the web API

baseURL + "/PostDegreeYears", // "/PutOrgSchool",

In the server side consider the below code.

public void Create(ABSModel model)
        {
            try
            {
                using (context = new Pinc_DBEntities())
                {

                        tblAB tb = new tblAB();
                        tb.ABS = model.ABS;
                        tb.AreaFacility = model.AreaFacility;
                        context.tblABS.Add(tb);

                        Save();
                        model.ABSID = tb.ABSID;//this is the important line of code, you are returning the just inserted record's id (primary key) back to the kendo grid after successfully saving the record to the database.


                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

please adjust the above example according to your requirement.

You can attached and respond to the requestEnd event on the DataSource.

requestEnd: function(e) {
        if (e.type === "create") {
            this.read();
        }
    }

What that is saying is: "After you created a record, reread the entries (including the newly created one) into the grid". Thereby giving you the newly created entry with key and all. Of course you see that the extra read may have some performance issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!