I try to change data in a form and then save. Instead of changing it adds a record..
This is my script:
$('#UpdateObject').click(function () {
var ObjnameEntered = $("#NameOB")[0].value;
var BasenameEntered = $("#selbaseID")[0].value;
var idsave = $("#labelobID")[0].value;
var kaartX = $("#mapx")[0].value;
var kaartY = $("#mapy")[0].value;
var picture = $("#trainpicname")[0].value;
$.ajax({
url: '/api/Traininglocation/' + idsave +'/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ TrainLocationName: ObjnameEntered, TrainBaseID: BasenameEntered, TrainXcoord: kaartX, TrainYcoord: kaartY, TrainPicture: picture }),
dataType: 'json',
success: $('#doebam').show()
});
});
The controller: i used the put methode then i get a 405 error after publish my project..
error on the server hosing : Event code: 4005 Event message: Forms authentication failed for the request. Reason: The ticket supplied was invalid. Event time: 2-12-2013 16:21:56 Event time (UTC): 2-12-2013 15:21:56 Event ID: 817a9928807646699d3d2ef795ae478d Event sequence: 2 Event occurrence: 1 Event detail code: 50201
// PUT api/Traininglocation/5
public HttpResponseMessage PutTrainLocation(int id, TrainLocation trainlocation)
{
if (ModelState.IsValid && id == trainlocation.id_trainlocation)
{
db.TrainLocation.Attach(trainlocation);
db.ObjectStateManager.ChangeObjectState(trainlocation, EntityState.Modified);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// POST api/Traininglocation
public HttpResponseMessage PostTrainLocation(TrainLocation trainlocation)
{
if (ModelState.IsValid)
{
db.TrainLocation.AddObject(trainlocation);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, trainlocation);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = trainlocation.id_trainlocation }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Who can help me?
I changed my controller : where PutTrainLocation to PostTrainLocation. Thanks for response!!
public HttpResponseMessage PostTrainLocation(int id, TrainLocation trainlocation)
{
if (ModelState.IsValid && id == trainlocation.id_trainlocation)
{
db.TrainLocation.Attach(trainlocation);
db.ObjectStateManager.ChangeObjectState(trainlocation, EntityState.Modified);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}