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

Post to WebAPI and ODATA resulting in multiple posts

1 Answer 31 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
CM
Top achievements
Rank 1
CM asked on 28 Jun 2015, 11:50 PM

Hey everyone,

I have an issue with my Data Source that when I post, it's not updating the Data Source's entity with the new ID that is created on the server side. What then happens, is any other post I make sends multiple posts. The second time I post, it sends two posts, the third time, three posts, etc. I have look through the forum post here:

 http://www.telerik.com/forums/kendo-ui-grid-inserts-updates-create-duplicate-records#2063733

And I'm having the same issue. I am returning the Id of the newly inserted record, and it's even saving out to the SQL data base.

I'm using ASP.NET WebAPI Odata controllers. The controller looks like this:

// POST: odata/Actions
public IHttpActionResult Post(TNYData.Action action)
    {
      if (!ModelState.IsValid)
      {
        return BadRequest(ModelState);
      }
 
      action.Created = DateTime.Now;
      action.CreatedBy = User.Identity.Name;
      action.Modified = DateTime.Now;
      action.ModifiedBy = User.Identity.Name;
 
      db.Actions.Add(action);
      db.SaveChanges();
 
      return Created(action);
    }

The Data Source looks like this:

actionDataSource: new kendo.data.DataSource({
      type: "odata-v4",
      autoSync: true,
      transport: {
        read: {
          url: config.apiBasePath + "Actions",
          dataType: "json"
        },
        create: {
          url: config.apiBasePath + "Actions",
          dataType: "application/json",
          beforeSend: function(req) {
            req.setRequestHeader('Authorization', sessionStorage.getItem("tnyApiKey"));
          }
        }
      },
      schema: {
        data: kendo.data.schemas["odata-v4"].data,
        total: kendo.data.schemas["odata-v4"].total,
        model: {
          id: "Id",
          fields: {
              Id: {
              //this field will not be editable (default value is true)
              editable: false,
              // a defaultValue will not be assigned (default value is false)
              nullable: true
            },
            Name: {
              validation: { required: true }
            },
            Description: {
              validation: { required: false }
            }
          }
        }
      }
    })

The response JSON look like this:

{
  "odata.metadata":"http://localhost:64533/odata/$metadata#Actions/@Element","Id":367,"Name":"New Item","Description":null,"Created":"2015-06-28T19:37:14.9379909-04:00","Modified":"2015-06-28T19:37:14.9379909-04:00","CreatedBy":null,"ModifiedBy":null,"StartDate":null,"DueDate":null,"Flagged":false
}

I get no errors in Chrome console, IE, or Visual Studio.

What am I missing here?

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 01 Jul 2015, 06:28 AM

Hello CM,

Unfortunately, I do not see anything suspicious in the code snippets you have provided. Therefore, it will be appreciated if you could provide a small runnable sample which demonstrates the issue.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
CM
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or