角度选择值字符串解析为整数 [英] Angular select value string parsing to integer

查看:22
本文介绍了角度选择值字符串解析为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从角度选择控件中获取选定的值到 Asp.net Core 3.0 WebApi.当我调试时,我发现所选值是字符串格式,但我的数据模型接受整数.我收到以下错误

<块引用>

JSON 值无法转换为 System.Int32.路径:$.bankID |行号:0 |字节位置内联:77.

是否有一种简单的方法可以将字符串值从选择控件转换为整数.

角度代码

组件.ts

导出类 BankAccountComponent 实现 OnInit {构造函数(私人FB:FormBuilder,私人银行服务:银行服务,私人服务:BankAccountService) { }bankAccountForms: FormArray = this.fb.array([]);银行列表 = [];ngOnInit() {this.bankService.getBankList().subscribe( res => this.bankList = res as []);this.addBankAccountForm();this.service.getBankAccountList().subscribe(资源=>{如果 (res == []) {this.addBankAccountForm();}别的 {//根据从BankAccont 表接收到的数据生成formarray(res as []).forEach((bankAccount: any) => {this.bankAccountForms.push(this.fb.group({bankAccountID: [bankAccount.bankAccountID],accountNumber: [bankAccount.accountNumber, Validators.required],accountHolder: [bankAccount.accountHolder, Validators.required],bankID: [bankAccount.bankID, Validators.min(1)],IFSC: [bankAccount.ifsc, Validators.required]}));});}});}addBankAccountForm(){this.bankAccountForms.push(this.fb.group({银行账户 ID: [0],accountNumber: ['', Validators.required],accountHolder: ['', Validators.required],bankID: [0, Validators.min(1)],//这里我们使用下拉菜单IFSC: ['', Validators.required],}));}记录提交(fg:FormGroup){(fg.value.bankAccountID == 0)this.service.postBankAccount(fg.value).subscribe((res: 任何) =>{fg.patchValue({ bankAccountID: res.bankAccountID });//this.showNotification('插入');});}}

组件.html

<div class="td"><input class="form-control" formControlName="accountNumber">

<div class="td"><input class="form-control" formControlName="accountHolder">

<div class="td"><select class="form-control" formControlName="bankID"><option value="0">选择</option><option *ngFor="let item of bankList" value="{{item.bankID}}">{{item.bankName}}</option></选择>

<div class="td"><input class="form-control" formControlName="IFSC">

<div class="td"><button class="btn btn-success" [disabled]="fg.invalid"><i class="far fa-save fa-lg"></i>提交

</表单>

service.ts

 postBankAccount(formData) {返回 this.http.post(environment.apiBaseURI + '/BankAccount', formData);}

组件.tsWebApi 代码
Webapi/模型

公共类银行账户{[钥匙]公共 int BankAccountID { 获取;放;}[最大长度(20)][必需的]公共字符串 AccountNumber { 获取;放;}[最大长度(100)][必需的]公共字符串 AccountHolder { 获取;放;}[必需的]公共 int BankID { 获取;放;}[最大长度(20)][必需的]公共字符串 IFSC { 获取;放;}}

WebApi 控制器

[HttpPost]公共异步任务>PostBankAccount(BankAccount bankAccount){_context.BankAccounts.Add(bankAccount);等待 _context.SaveChangesAsync();return CreatedAtAction("GetBankAccount", new { id = bankAccount.BankAccountID }, bankAccount);}

解决方案

如果我正确理解你想要做什么,我有 2 个解决方案

  1. 将数据发送到服务器时将值更改为数字

recordSubmit(fg: FormGroup) {如果(fg.value.bankAccountID == 0){fg.value.bankID = +fg.value.bankID;//+ 运算符会将类型更改为数字this.service.postBankAccount(fg.value).subscribe((res: 任何) =>{fg.patchValue({ bankAccountID: res.bankAccountID });//this.showNotification('插入');});}}

  1. change the value to number when the select value will be changed在 html 中

 
                
            
发送“验证码”获取 | 15天全站免登陆