Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用PUT时角度-400错误(错误请求)_C#_Angular - Fatal编程技术网

C# 使用PUT时角度-400错误(错误请求)

C# 使用PUT时角度-400错误(错误请求),c#,angular,C#,Angular,在Angular程序中,我试图将用户从我的表中输入的行发布到我的数据库。但是,每当我在控制台中使用put时,我都会得到一个错误 PUT 400(错误请求) 我从服务器得到的响应是 {“Message”:“请求无效。”,“MessageDetail”:“参数字典包含方法”System.Web.Http.IHttpActionResult puttodata(Int32,PTOTracker.Models.PTOData)的不可空类型“System.Int32”的参数“id”的空条目”'在'PTOT

在Angular程序中,我试图将用户从我的表中输入的行发布到我的数据库。但是,每当我在控制台中使用put时,我都会得到一个错误

PUT 400(错误请求)

我从服务器得到的响应是

{“Message”:“请求无效。”,“MessageDetail”:“参数字典包含方法”System.Web.Http.IHttpActionResult puttodata(Int32,PTOTracker.Models.PTOData)的不可空类型“System.Int32”的参数“id”的空条目”'在'PTOTracker.Controllers.PTODataController'中。可选参数必须是引用类型、可为null的类型,或者声明为可选参数。}

它说我没有输入身份证,但我以为我输入了

这是我的模型:

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);
}
保存(pto:PTOData):承诺{
返回此文件。http
.put(this.ptoDateUrl+'/'+pto.ID、pto、this.options)
.toPromise()
.then(res=>res.json().data作为PTOData)
.接住(这个.把手错误);
}
这是我的数据控制器:

使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web.Http;
使用System.Web.Http.Description;
使用PTOTracker.Models;
名称空间跟踪程序
{
公共类数据控制器:ApiController
{
private PTOTrackerContext db=new PTOTrackerContext();
//获取:api/PTOData
公共IQueryable GetPTODatas()
{
返回db.PTODatas;
}
//获取:api/PTOData/5
[响应类型(类型(数据))]
公共IHttpActionResult GetPTOData(int id)
{
PTOData PTOData=db.PTODatas.Find(id);
if(pTOData==null)
{
返回NotFound();
}
返回Ok(数据);
}
//PUT:api/PTOData/5
[HttpPut]
[响应类型(typeof(void))]
[路由(“api/PTOData/{id}”)]
公共IHttpActionResult PutPTOData(int-id,PTOData-PTOData)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
if(id!=pTOData.id)
{
返回请求();
}
db.Entry(pTOData).State=EntityState.Modified;
尝试
{
db.SaveChanges();
}
catch(DbUpdateConcurrencyException)
{
如果(!PTODataExists(id))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回状态码(HttpStatusCode.NoContent);
}
//职位:api/PTOData
[响应类型(类型(数据))]
公共IHttpActionResult PostToData(PTOData PTOData)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
db.PTODatas.Add(pTOData);
db.SaveChanges();
返回CreatedAtRoute(“DefaultApi”,新的{id=pTOData.id},pTOData);
}
//删除:api/PTOData/5
[响应类型(类型(数据))]
公共IHttpActionResult DeletePTOData(内部id)
{
PTOData PTOData=db.PTODatas.Find(id);
if(pTOData==null)
{
返回NotFound();
}
db.PTODatas.Remove(pTOData);
db.SaveChanges();
返回Ok(数据);
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
db.Dispose();
}
基地。处置(处置);
}
私有布尔存在(int-id)
{
返回db.PTODatas.Count(e=>e.ID==ID)>0;
}
}

}
问题似乎在于:

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

问题似乎是这样的:

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

将记录添加到数据库的代码不正确

首先,根据camaron的解决方案更改JS方面的内容

然后在服务器端,需要将实体添加到数据库中。有一个很好的样品

db.Entry(pTOData).State = EntityState.Modified;
需要更改为:

db.PTODatas.Add(pTOData);
return Ok(pTOData);
以及:

需要更改为:

db.PTODatas.Add(pTOData);
return Ok(pTOData);

将记录添加到数据库的代码不正确

首先,根据camaron的解决方案更改JS方面的内容

然后在服务器端,需要将实体添加到数据库中。有一个很好的样品

db.Entry(pTOData).State = EntityState.Modified;
需要更改为:

db.PTODatas.Add(pTOData);
return Ok(pTOData);
以及:

需要更改为:

db.PTODatas.Add(pTOData);
return Ok(pTOData);

您忘记添加Web API代码了。您的角度代码和模型定义与此问题无关看起来像
这个。newRow
没有ID。如果您的数据库有自动增量键,则看起来您在数据库中执行错误的插入。我们需要查看您的后端服务function@mjwills更新了OPIf you
console.log(this.ptoDateUrl+'/'+pto.ID)
在调用
this.http
之前,控制台中写入了什么?您忘记添加Web API代码。角度代码和模型定义与此问题无关。如
所示。newRow
没有ID。如果数据库具有自动增量键,则看起来您执行了错误的插入操作您的数据库。我们需要查看您的后端服务function@mjwills更新了OPIf您
console.log(this.ptoDateUrl+'/'+pto.ID);