我的Web Api中有例外
对象引用未设置为对象的实例。
对象中的数据将打印到控制台上,但不会传递到Api控制器。对象ae的“空”
这是我的控制者:
[Route("api/Addmovie")]
[HttpPost]
public int Addmovie([FromBody]Movie movie)
{
int check = objMovie.Addmovie(movie);
return check;
}
Addmovie是我创建的用于将数据存储在数据库中的功能。
这是我的组件:
import { FormGroup,FormControl,Validators,FormBuilder } from '@angular/forms';
import { slideInOutAnimation } from 'src/app/animations';
import { Movie } from 'src/app/movie';
import { NgxSpinnerService} from 'ngx-spinner';
import { MovieServiceService } from 'src/app/Service/movie-service.service';
import { HttpClient } from '@angular/common/http';
import { formatNumber } from '@angular/common';
@Component({
selector: 'app-addmovie',templateUrl: './addmovie.component.html',styleUrls: ['./addmovie.component.css'],// make slide in/out animation available to this component
animations: [slideInOutAnimation],// attach the slide in/out animation to the host (root) element of this component
// tslint:disable-next-line: no-host-metadata-property
host: { '[@slideInOutAnimation]': '' }
})
export class AddmovieComponent implements OnInit {
// tslint:disable-next-line: new-parens
movie = new Movie;
fileData: File = null;
addmovieForm: FormGroup;
constructor(
private spinner: NgxSpinnerService,public fb: FormBuilder,private http: HttpClient,public movieService: MovieServiceService) {
this.addmovieForm = this.fb.group ({
movieName: new FormControl('',[Validators.required,Validators.minLength(1)]),releaseDate: new FormControl('',[Validators.required]),releaseYear: new FormControl('',certification: new FormControl('',runtime: new FormControl('',rating: new FormControl('',Validators.max(10)]),moviePlot: new FormControl('',cast: new FormControl('',imageName: new FormControl('',[Validators.required])
});
}
ngOnInit() {
}
onFileSelect(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0];
this.addmovieForm.get('imageName').setvalue(file);
}
}
public onSubmit() {
debugger;
// tslint:disable-next-line: prefer-const
let movieForm = this.addmovieForm.value;
this.movie.movieName = movieForm.movieName;
this.movie.releaseDate = movieForm.releaseDate;
this.movie.releaseYear = movieForm.releaseYear;
this.movie.certification = movieForm.certification;
this.movie.runtime = movieForm.runtime;
this.movie.review = movieForm.rating;
this.movie.moviePlot = movieForm.moviePlot;
this.movie.cast = movieForm.cast;
this.movie.imageName = movieForm.imageName;
console.log(this.movie);
this.http.post('http://localhost:50686/api/Addmovie',this.movie).subscribe(
(res) => console.log(res),(err) => console.log(err)
);
}
}