2017-06-22 99 views
0

在我的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; 
 
     } 
 
    } 
 
}

+2

你忘了添加Web API代碼:以

return StatusCode(HttpStatusCode.NoContent); 

需要改變。您的角碼和模型定義與此問題無關 –

+0

看起來像'this.newRow'沒有ID。如果你的數據庫有一個自動增量鍵,它看起來像是你的數據庫執行了錯誤的插入。 – Raven

+1

我們需要看你的後端服務功能 – Ferus7

回答

1

你的代碼的記錄添加到數據庫中是不正確的。

首先,根據camaron的解決方案改變JS的一面。

然後在服務器端,您需要將您的實體添加到數據庫。 https://stackoverflow.com/a/22222636/34092有一個很棒的例子。

db.Entry(pTOData).State = EntityState.Modified; 

需要改爲:

db.PTODatas.Add(pTOData); 

和:

return Ok(pTOData); 
2

這個問題似乎是這樣的:

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 
     }) 
    }) 

}

你推你的新對象,你提出的要求後,不是之前。 應該是這樣的:

this.ptoDataService.save({ 
       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}) 
    .then((response: any) => { //do what you want with the response.})