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?