Post selectlist item from Angular to ASP.NET Core API

36 Views Asked by At

Posting selectlist information does not work and gives a null error.

I tried to send a form with a selectlist. Without the selectlist the form is working well. When I try to send the selectlist information, I get a null error (and on the Angular side an internal server error).

The following code on the API side:

#region httpPost
[HttpPost]
public async Task<IActionResult> AddReport([FromBody] Report reportRequest)
{
    var ReportNo = _context.Reports.Select(c => c.ReportNo).Count();
    var nextReportNo = ReportNo += 1;
    var date = DateTime.Now;

    reportRequest.ID = Guid.NewGuid();
    reportRequest.DateCreated = date;
    reportRequest.ReportNo = nextReportNo;

    await _context.Reports.AddAsync(reportRequest);
    await _context.SaveChangesAsync();

    return Ok(reportRequest);
}

The report model on the Angular side:

export interface Report {
    id: string;
    reportNo: number;
    dateCreated: Date;
    dateEvent: Date; //Date of event
    title: string;
    issuedBy: string;
    description: string;
    isActive: boolean; // Check if the report is active
    closedOnDate: Date;
    editOnDate: Date;
    reportByEmployeeOrVisitor: string;
    actionNeeded: boolean;
    closeReport: boolean;
    Building: string;
    buildingName: string;
    KindOfReport: string;
    KindOfReportName: string;

This is the angular code for adding the information:

export class AddReportComponent implements OnInit {
  currentDate = new Date();
  SelectedKindOfReport:string = '';
  SelectedBuilding:string= '';
  kindOfReports: KindOfReport[]=[
  ];

  buildings: Building[]=[
  ];

  addReportRequest: Report = {
 id: ' ',
  KindOfReport: this.SelectedKindOfReport,
  buildingName: '',
  KindOfReportName: '',
   reportNo: 0,
  closedOnDate: new Date(),
  Building: this.SelectedBuilding,
    issuedBy: ' ',
    dateCreated: this.currentDate,
    dateEvent:  this.currentDate,
    editOnDate: new Date(),
    title: ' ',
    description: ' ',
    isActive: true,
    closeReport: false,
    actionNeeded: false,
    reportByEmployeeOrVisitor: '',
  };
  constructor(private ReportService: ReportsService, private datePipe : DatePipe, private router: Router) {}
  ngOnInit(): void {
    const cValue = formatDate(this.currentDate, 'yyyy-MM-dd', 'en-US');
    this.SelectedKindOfReport = "3fa85f64-5717-4562-b3fc-2c963f66afa6";
    this.SelectedBuilding= "2feecbc8-ed43-4c49-829f-c49dd707f75c";
0

There are 0 best solutions below