我尝试更改表单中的数据然后保存。而不是改变它添加记录..
这是我的剧本:
$('#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()
});
});
控制器: 我使用put方法然后在发布我的项目后得到405错误..
服务器软管上的错误: 活动代码:4005 事件消息:请求的表单身份验证失败。原因:提供的票证无效。 活动时间:2013年12月12日16:21:56 活动时间(UTC):2013年12月12日15:21:56 事件ID:817a9928807646699d3d2ef795ae478d 事件顺序:2 事件发生:1 活动详情代码: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);
}
}
谁能帮帮我?
答案 0 :(得分:0)
我改变了我的控制器: 其中PutTrainLocation到PostTrainLocation。 谢谢你的回复!!
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);
}
}