简体   繁体   English

如何在Angular 6和.NET Core中按ID设置表单中的值?

[英]How to set values in forms by Id in Angular 6 and .NET core?

I want to edit a specific car and I have made a method for it. 我要编辑特定的汽车,并且已经为此制定了一种方法。

This is my code in the CarController.cs 这是我在CarController.cs中的代码

[Route("GetCarById")]
[HttpGet]
public async Task<Car> Get(long carId)
{
return await _context.Cars.FirstAsync(e => e.CarId == carId);
}

Then I call this method in my carServices.js 然后我在carServices.js中调用此方法

getCarById(carId: number): Observable<CarVM> {
    return this.http.get<CarVM>(this.baseUrl + "/getCarById?carId=" + carId)
  }

And finally I call this method in my car component 最后,我在汽车部件中调用此方法

 EditCar(CarId: number) {
    this.carservice.getCarById(CarId).subscribe();
  }

Then I bind this method on a button like this: 然后,将这种方法绑定到这样的按钮上:

<button type="button" class="btn btn-primary mr-1" (click)="EditCar(car.carId)">Edit</button>

I have debug my application and I get the right values when I click on the edit button. 我已经调试了我的应用程序,并且当我单击编辑按钮时我得到了正确的值。 Now I want to bind these values in my text fields. 现在,我想将这些值绑定到我的文本字段中。 I use Formbuilder and in the past I used ng-model . 我使用Formbuilder ,过去使用ng-model This is the whole code for the forms: 这是表格的完整代码:

 <form [formGroup]="FormCar" (ngSubmit)="AddCar(FormCar.value)">
    <div class="row">
      <div class="form-group col-sm-3">
        <label>Brand</label>
        <input type="text" class="form-control" formControlName="Brand" id="brand" placeholder="Enter brand">
      </div>
    </div>
    <div class="row">
      <div class="form-group col-sm-3">
        <label>Model</label>
        <input type="text" class="form-control" formControlName="Model" id="model" placeholder="Enter model">
      </div>
    </div>
    <div class="row">
      <div class="form-group col-sm-3">
        <label>Color</label>
        <input type="text" class="form-control" formControlName="Color" id="color" placeholder="Enter color">
      </div>
    </div>
    <div class="row">
      <div class="form-group col-sm-3">
        <label>Speed</label>
        <input type="number" class="form-control" formControlName="TopSpeed" id="topSpeed" placeholder="Enter speed">
      </div>
    </div>
    <div class="row">
      <div class="form-group col-md-2">
        <button type="submit" class="btn btn-danger mr-1">Save changes</button>
      </div>
      <div class="form-group col-md-2">
        <button type="reset" class="btn btn-danger mr-1" (click)="reset()">New record</button>
      </div>
    </div>
  </form>

I have used to set the values for each field like this but it didn't work for me: 我曾经像这样设置每个字段的值,但对我来说不起作用:

this.Form.controls['Brand'].setValue(Response.Brand); 

Can someone explain me how to set the values from the web api? 有人可以向我解释如何通过网络API设置值吗?

UPDATE UPDATE

This is my code for the Formbuilder 这是我的Formbuilder代码

 buildFormCar() {
    this.FormCar = this.formBuilder.group({
      CarId: ['', Validators.required],
      Brand: ['', Validators.required],
      Model: ['', Validators.required],
      Color: ['', Validators.required],
      TopSpeed: ['', Validators.required],
    });
  }

Use like this: 像这样使用:

EditCar(CarId: number) {
    this.carservice.getCarById(CarId).subscribe((resp: any) => {
        this.FormCar.patchValue({
          CarId: resp.carId,
          Brand: resp.brand,
          Model: resp.model
        })
    });
  }

Try using patchValue like this: 尝试像这样使用patchValue

  EditCar(CarId: number) {
    this.carservice.getCarById(CarId).subscribe((resp: any) => {
        this.FormCar.patchValue(res)
    });
  }

You can also use setValue instead of patchValue id all form controls are present in res. 您也可以使用setValue而不是patchValue id,所有表单控件都存在于res中。

Since in your backend response, the first letter of properties is in lowercase, its better if you build the formBuilder in the same way, 由于在后端响应中,属性的首字母是小写字母,因此如果您以相同的方式构建formBuilder会更好,

buildFormCar() {
    this.FormCar = this.formBuilder.group({
      carId: ['', Validators.required],
      brand: ['', Validators.required],
      model: ['', Validators.required],
      color: ['', Validators.required],
      topSpeed: ['', Validators.required],
    });
  }

and assign in the same way in template: 并以相同的方式在模板中进行分配:

<input type="text" class="form-control" formControlName="brand" id="brand" placeholder="Enter brand">

This will shorten your code and you won't have to reasign all the propertied during edit. 这将缩短您的代码,并且您不必在编辑过程中重新分配所有资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM