Reputation: 381
My Web API Put method is called properly from my jQuery Ajax but the C# code does not save the update. The json object does not have all the properties of the MEMBERS Entity. The context does not save any changes to the database.
public void Update(string id, MEMBERS obj)
{
var memToUpdate = context.MEMBERS.Find(obj.MEMBERID);
if (memToUpdate != null)
{
context.Entry(memToUpdate).CurrentValues.SetValues(obj);
int result = context.SaveChanges();
System.Diagnostics.Debug.WriteLine("save result:" + result);
}
}
This code will work but How do I make context update all the properties of the MEMBERS Entity that are in the JSON Object without specifying like this?
public void Update(string id, MEMBERS obj)
{
MEMBERS memToUpdate = context.MEMBERS.Find(obj.MEMBERID);
if (memToUpdate != null)
{
//context.Entry(memToUpdate).CurrentValues.SetValues(obj);
memToUpdate.FIRSTNAME = obj.FIRSTNAME;
memToUpdate.LASTNAME = obj.LASTNAME;
int result = context.SaveChanges();
System.Diagnostics.Debug.WriteLine("save result:" + result);
}
}
Jquery:
var data = {
MEMBERID: "B745",
FIRSTNAME: "TESTPUT",
LASTNAME: "UPDATED WEBAPI"
};
var json = JSON.stringify(data)
$.ajax({
url: 'api/Members/' + data.MEMBERID,
type: 'PUT',
contentType: "application/json; charset=utf-8",
data: json,
success: function (results) {
alert("Success");
}
})
My question is more about how to do Web API Put partial update. I read about the Patch method using Delta<> type from oData. I installed Web API oData from nuget but VS complains type or namespace Delta does not exist. Has anyone run into this problem?
Upvotes: 0
Views: 4894
Reputation: 6282
You can do partial updates using WebApi without using Odata (you do need to install the libary but the Delta object seems to work fine without the oData stuff). I would use PATCH and not PUT as from my understanding PUT is there to replace the whole resource, PATCH is the partial HTTP Verb.
You need the odata library (but just for the Delta library, i hope they move this out of here and make it more general as it will be useful) http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData
Some code, not sure if this will 100% work, i have a slightly different setup. I do use something similar to your original code to facilitate my PUT, but only because the Delta.Put method throws loads of errors, which i think is to do with my dates, anyhoo, not related.
public void Update(string id, Delta<MEMBERS> obj)
{
var memToUpdate = context.MEMBERS.Find(obj.MEMBERID);
if (memToUpdate != null)
{
obj.Patch(memToUpdate);
int result = context.SaveChanges();
System.Diagnostics.Debug.WriteLine("save result:" + result);
}
}
Upvotes: 1
Reputation: 381
It looks like WEB API cannot do partial update unless using oData. I ended up having to call the GET method to get back the whole object with all properties then changed the value of some of the properties I want and update via PUT method.
$(document).ready(function () {
$.getJSON('api/Members/B745', function (data) {
data.FirstName = 'Test Put Success';
data.LastName = 'My Lastname Is';
var json = JSON.stringify(data)
$.ajax({
url: 'api/Members/' + data.MEMBERID,
type: 'PUT',
contentType: "application/json; charset=utf-8",
data: json,
success: function (results) {
alert("Success");
}
})
});
});
Upvotes: 0