1

I have c# controller

[EnableCors("AllowSpecificOrigin")]
        [Route("AddCar"), HttpPost]
        public async Task<CarStatus> AddCar(Car car)
        {
            return _carService.AddCar(car);
        }

and Angular services

  AddCar(addCarModel: CarModel): Promise<CarStatus>{

const headers =  new Headers({ 'Content-Type': 'application/json'});
return this.http.post("http://localhost:54116/AddCar", JSON.stringify({RegistrationNumber : addCarModel.RegistrationNumber, TypeOfCar: addCarModel.TypeOfCar, Model: addCarModel.Model,
YearOfProduction: addCarModel.YearOfProduction, Power: addCarModel.Power, vinNumber: addCarModel.VinNumber, Factory: addCarModel.Factory, CarReviewDate: addCarModel.CarReviewDate ,
OcEndDate: addCarModel.OcEndDate, Insurer: addCarModel.Insurer,UdtElevatorReviewWhen : addCarModel.UdtElevatorReviewWhen, UdtElevatorReviewFrom: addCarModel.UdtElevatorReviewFrom, 
 TachografReviewWhen: addCarModel.TachografReviewWhen, TachografReviewFrom: addCarModel.TachografReviewFrom,FaultList: null ,Owner: addCarModel.Owner}), { headers: new Headers({ 'Content-Type': 'application/json' }) }).toPromise().
 then(response => {
  var y = response.json();
  return y;

}); and AddCar.ts

import { Component, OnInit } from '@angular/core';
import { CarService } from '../../services/car.service';
import { NgForm } from "@angular/forms";

@Component({
  selector: 'app-add-car',
  templateUrl: './add-car.component.html',
  styleUrls: ['./add-car.component.css']
})
export class AddCarComponent implements OnInit {

  constructor(private carService: CarService) { }

  ngOnInit() {
  }

  onSubmit(form: NgForm): void {
    console.log(form.value);
    this.carService.AddCar(form.value);
  }
}

My Angular CarModel

export class CarModel{
    RegistrationNumber: string;
    CarReviewDate : Date;
    Factory: string;
    Insurer: string;
    Model : string;
    OcEndDate: Date;
    Owner : string;
    Power : number;
    TypeOfCar: string;
    YearOfProduction: number;
    VinNumber: string;
    UdtElevatorReviewFrom: Date;
    UdtElevatorReviewWhen: Date;
    TachografReviewFrom: Date;
    TachografReviewWhen: Date;
    FaultList : Fault[];
}

My c# Car Model

public class Car
    {
        [Key]
        public string RegistrationNumber { get; set; }
        public string TypeOfCar { get; set; }
        public string Model { get; set; }
        public int YearOfProduction { get; set; }
        public int Power { get; set; }
        public string VinNumber { get; set; }
        public string Factory { get; set; }
        public DateTime CarReviewDate { get; set; }
        public DateTime OcEndDate { get; set; }
        public string Insurer { get; set; }
        public DateTime? UdtElevatorReviewWhen { get; set; }
        public DateTime? UdtElewatorReviewFrom { get; set; }
        public DateTime? TachografReviewWhen { get; set; }
        public DateTime? TachografReviewFrom { get; set; }
        public List<Fault> FaultList { get; set; }
        public string Owner { get; set; }

In back end i have braekpoint and connection is good, but all values in sending car are nulls. Why? and how can i fix that? link to project https://github.com/BialekM/TransportApp network status https://i.stack.imgur.com/iziOO.jpg C# inform me the all values are null without 2 datetime wchick cant be null

  • 2
    This is usually a case of the JSON graph not matching the type in c#. The information in the question is not enough to be able to tell. You should capture the actual JSON being generated and see if it would deserialize to a car. – Crowcoder Oct 22 '17 at 12:06
  • if your `CarModel` on the frontend has EXACTLY the same properties as `Car` in your backend just use `JSON.stringify(addCarModel)` normally, the reason for getting null at the backend is because the object you're sending does not match the backend object – Elmer Dantas Oct 22 '17 at 12:12
  • Please use Chrome Developer Tools (Network tab) and get the (JSON?) payload being sent to the server. Please include that payload in your question. – mjwills Oct 22 '17 at 12:30
  • Ok this is my chrome network results https://imgur.com/QRZ4KJw and this is my github project link https://github.com/BialekM/TransportApp – Michał Białek Oct 23 '17 at 08:00
  • all values of Car in back-end are null without 2 datetimes which cant be null – Michał Białek Oct 23 '17 at 15:59
  • My c# read json results https://imgur.com/bf0dPVR – Michał Białek Oct 23 '17 at 16:07
  • Are you using MVC? WebAPI? ASP.NET Core? – mjwills Oct 23 '17 at 22:34
  • I Use MVC Asp.Net Core – Michał Białek Oct 24 '17 at 05:34
  • 1
    Possible duplicate of [Asp.net core MVC post parameter always null](https://stackoverflow.com/questions/38455954/asp-net-core-mvc-post-parameter-always-null) – mjwills Oct 25 '17 at 08:58

0 Answers0