在我的Angular程序中,我试图将用户从我的表中输入的行发布到我的数据库。但是,每当我用放,在控制台中,我得到的是在说使用PUT时角度 - 400错误(错误请求)
PUT 400 (Bad Request)
和响应一个错误,我从服务器获取是
{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult PutPTOData(Int32, PTOTracker.Models.PTOData)' in 'PTOTracker.Controllers.PTODataController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
这是说我没有输入身份证,但我认为我是。
这里是我的模型:
namespace PTOTracker.Models
{
public class PTOData
{
[Key]
public int ID { get; set; }
public int EmpKey { get; set; }
public string type { get; set; }
public DateTime date { get; set; }
public string fullhalf { get; set; }
public int hours { get; set; }
public string scheduled { get; set; }
public string notes { get; set; }
public bool inPR { get; set; }
public DateTime? prDate { get; set; }
} }
,这里是我从我的组件保存功能:
saveNewRow(): void {
this.ptoDataService.save(this.newRow)
.then(PTOData => {
this.ptoData.push({
ID: 123456,
EmpKey: this.empInfo[this.selectedEmployee].EmpKey,
type: this.selectedType,
date: this.newRow.date,
fullhalf: this.newRow.fullhalf,
hours: this.newRow.hours,
scheduled: this.newRow.scheduled,
notes: this.newRow.notes,
inPR: (this.newRow.inPR ? true : false),
prDate: this.newRow.prDate
})
})
}
,这里是我在我的服务保存功能:
save(pto: PTOData): Promise<PTOData> {
return this.http
.put(this.ptoDateUrl + '/' + pto.ID, pto, this.options)
.toPromise()
.then(res => res.json().data as PTOData)
.catch(this.handleError);
}
这里是我的PTODataController:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using PTOTracker.Models;
namespace PTOTracker.Controllers
{
public class PTODataController : ApiController
{
private PTOTrackerContext db = new PTOTrackerContext();
// GET: api/PTOData
public IQueryable<PTOData> GetPTODatas()
{
return db.PTODatas;
}
// GET: api/PTOData/5
[ResponseType(typeof(PTOData))]
public IHttpActionResult GetPTOData(int id)
{
PTOData pTOData = db.PTODatas.Find(id);
if (pTOData == null)
{
return NotFound();
}
return Ok(pTOData);
}
// PUT: api/PTOData/5
[HttpPut]
[ResponseType(typeof(void))]
[Route("api/PTOData/{id}")]
public IHttpActionResult PutPTOData(int id, PTOData pTOData)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != pTOData.ID)
{
return BadRequest();
}
db.Entry(pTOData).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!PTODataExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/PTOData
[ResponseType(typeof(PTOData))]
public IHttpActionResult PostPTOData(PTOData pTOData)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.PTODatas.Add(pTOData);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = pTOData.ID }, pTOData);
}
// DELETE: api/PTOData/5
[ResponseType(typeof(PTOData))]
public IHttpActionResult DeletePTOData(int id)
{
PTOData pTOData = db.PTODatas.Find(id);
if (pTOData == null)
{
return NotFound();
}
db.PTODatas.Remove(pTOData);
db.SaveChanges();
return Ok(pTOData);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool PTODataExists(int id)
{
return db.PTODatas.Count(e => e.ID == id) > 0;
}
}
}
你忘了添加Web API代码:以
需要改变。您的角码和模型定义与此问题无关 –
看起来像'this.newRow'没有ID。如果你的数据库有一个自动增量键,它看起来像是你的数据库执行了错误的插入。 – Raven
我们需要看你的后端服务功能 – Ferus7