c# - 为什么 Id 返回 null 而不是从数据库中返回下一个 Id?
问题描述
为什么 Id 返回 null 而不是从数据库中返回下一个 Id?我认为 Id 应该是数据库中可用的下一个 id。相反,它只是返回一个空值。下面是我的组件。每当 console.log(this.vehicle.id) 我得到 NaN 并且在浏览器上该值为空。我需要一些帮助来弄清楚我做错了什么。
export class VehicleFormComponent implements OnInit {
makes: any;
models: any[];
features: any;
vehicle: SaveVehicle = {
id: 0,
makeId: 0,
modelId: 0,
isRegistered: false,
features: [],
contact: {
name: '',
email: '',
phone: ''
}
};
constructor(
private route: ActivatedRoute,
private router: Router,
private vehicleService: VehicleService, private toastrService: ToastrService) {
route.params.subscribe(p => {
this.vehicle.id = +p['id'];
});
}
ngOnInit() {
console.log(this.vehicle.id.toFixed(1));
const sources = [
this.vehicleService.getMakes(),
this.vehicleService.getFeatures(),
];
if (this.vehicle.id) {
sources.push(this.vehicleService.getVehicle(this.vehicle.id));
}
forkJoin(sources).subscribe((data: any) => {
this.makes = data[0];
this.features = data[1];
if (this.vehicle.id) {
this.setVehicle(data[2]);
this.populateModels();
}
}, err => {
if (err.status == 404) {
this.router.navigate(['/']);
}
});
}
private setVehicle(v: Vehicle) {
this.vehicle.id = v.id;
this.vehicle.makeId = v.make.id;
this.vehicle.modelId = v.model.id;
this.vehicle.isRegistered = v.isRegistered;
this.vehicle.contact = v.contact;
this.vehicle.features = _.pluck(v.features, 'id');
}
onMakeChange() {
this.populateModels();
delete this.vehicle.modelId;
}
private populateModels() {
const selectedMake = this.makes.find((m: { id: number; }) => m.id == this.vehicle.makeId);
this.models = selectedMake ? selectedMake.models : [];
}
onFeatureToggle(featureId: number, $event: { target: { checked: any; }; }) {
if ($event.target.checked) {
this.vehicle.features.push(featureId);
} else {
const index = this.vehicle.features.indexOf(featureId);
this.vehicle.features.splice(index, 1);
}
}
submit() {
if (this.vehicle.id) {
this.vehicleService.update(this.vehicle)
.subscribe(x => {
this.toastrService.success('The vehicle was sucessfully updated.', 'Success');
});
} else {
this.vehicleService.create(this.vehicle)
.subscribe(x => console.log(x));
}
}
这是我为要添加到数据库的新车创建的端点
[HttpPost]
[Authorize]
public async Task<IActionResult> CreateVehicle([FromBody] SaveVehicleResource vehicleResource)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var vehicle = mapper.Map<SaveVehicleResource, Vehicle>(vehicleResource);
vehicle.LastUpdate = DateTime.Now;
repository.Add(vehicle);
await unitOfWork.CompleteAsync();
vehicle = await repository.GetVehicle(vehicle.Id);
var result = mapper.Map<Vehicle, VehicleResource>(vehicle);
return Ok(result);
}
这是 SaveVehicleresouce.cs
public class SaveVehicleResource
{
public int Id { get; set; }
public int ModelId { get; set; }
public bool IsRegistered { get; set; }
[Required]
public ContactResource Contact { get; set; }
public ICollection<int> Features { get; set; }
public SaveVehicleResource()
{
Features = new Collection<int>();
}
}
这是我正在使用的接口。我使 int 可以为空?
export interface Vehicle {
id: number;
model: KeyValuePair;
make: KeyValuePair;
isRegistered: boolean;
features: KeyValuePair[];
contact: Contact;
lastUpdate: string;
}
export interface SaveVehicle {
id?: number;
modelId: number;
makeId: number;
isRegistered: boolean;
features: number[];
contact: Contact;
}
解决方案
尝试移动
route.params.subscribe(p => {
this.vehicle.id = +p['id'];
});
ngOnInit()
,并将已经存在的逻辑放入订阅中。
如果ngOnInit()
生命周期事件在route.params
可观察对象触发之前触发,则 id 还不会被设置。
推荐阅读
- c++ - 如何在数组中搜索先前已输入的 int 值
- android - firebase sha1 在同一个项目中更改
- python - 将大量“线条”绘制为阴影或颜色强度
- javascript - 执行 ajax 调用时是否发送事件?
- mql4 - MT4通讯方式
- javascript - 在 Ext JS 中达到 maxAllowed 字符后阻止用户输入
- javascript - 函数(function(parameter){
- mongodb - MongoDB PartialFilterExpression 过滤器问题
- r - 如何对具有两个变量和出现频率的数据框进行聚类
- java - CommPortIdentifier.getPortIdentifiers() 返回空