我的前端有角度,后端在.net中。尝试在.net端调用post方法时遇到错误。我试图在邮递员中调用相同的方法,并得到了错误。不知道我在做什么错。下面是我在.net中的发布方法:
[HttpPost()]
public async Task<actionResult<AllItProjectsList>> PostAllItProjectsList(AllItProjectsList allItProjectsList)
{
_context.AllItProjectsList.Add(allItProjectsList);
await _context.SaveChangesAsync();
return CreatedAtaction("GetallItProjectsList",new { id = allItProjectsList.ProjectId },allItProjectsList);
}
当我从角度调用此方法时,出现以下错误:
将值{null}转换为类型'System.Int32'时出错。路径“ projectId”,第1行,位置437。”
以下是屏幕截图:
当我尝试在邮递员中使用以下URL时:
https://localhost:44328/api/AllItProjectsLists?Name=Test&Description=TestDec
我收到错误消息,指出ReferenceError:名称未定义
以下是屏幕截图:
allitprjectlists在我的模型类中定义。下面是定义:
使用系统; 使用System.Collections.Generic;
命名空间WebAPI.Models {
public partial class AllItProjectsList
{
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string SubType { get; set; }
public string ExecutiveSponsor { get; set; }
public string Division { get; set; }
public string Section { get; set; }
public string Status { get; set; }
public string AppDev { get; set; }
public double? AppDevPriority { get; set; }
public double? AppDevEffort { get; set; }
public string ProjectRequestDate { get; set; }
public string ProjectApprovalDate { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? ExpectedCompletionDate { get; set; }
public DateTime? actualCompletionDate { get; set; }
public string Team { get; set; }
public string SurveyId { get; set; }
public int ProjectId { get; set; }
}
}
我从angular到.net的通话如下:
AddProjectDetails(project: AllItProject) {
const httpoptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
return this.http.post<AllItProject>(ROOT_URL,project,httpoptions);
}
除ProjectId外,所有字段都不是必需的。 ProjectId是数据库中的标识字段。我不确定为什么angular会抱怨projectId为null,而邮递员会抛出名称引用错误。
以下是项目对象的值:
任何帮助将不胜感激。