【问题标题】:How do I fill a material dropdownlist in angular 7 from my c# web api?如何从我的 c# web api 填充 Angular 7 中的材料下拉列表?
【发布时间】:2019-11-12 16:39:52
【问题描述】:

我是 Angular 的新手,并试图弄清楚如何从 C# .NET Web API 中以 Angular 绑定下拉列表。这是我在我的应用程序中所做的:

MVC Web API 模型

[Table("tblShirt")]
public class Shirt
{
    [Key]
    public int ShirtID { get; set; }
    public string ShirtName { get; set; }
    public string ShirtCode { get; set; }
    public bool IsActive { get; set; }
}

Angular 使用的控制器方法

[HttpGet]
[AllowAnonymous]
public IEnumerable<Shirt> GetShirtCodes()
{
    var result = (from r in db.Shirts where r.IsActive == true orderby r.ShirtCode select r).ToList();
    return (result);
}

component.html

<mat-form-field style="width:inherit;">
  <mat-label>Shirt Code</mat-label>
    <mat-select>
      <mat-option *ngFor="let shirt of shirts" [value]="shirts.value">
        {{shirts.viewValue}}
      </mat-option>
     </mat-select>
 </mat-form-field>

除此之外,我还在自己的 model.ts 中镜像了我的模型。这就是我卡住的地方。我读过的所有教程都是从这里开始的。我在我的 component.ts 中尝试了以下内容

import { Component, OnInit } from '@angular/core';
import { ReprintService } from 'src/app/shared/reprint.service';
import { FormBuilder, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { Reprint } from 'src/app/shared/reprint.model';
import { NotificationService } from 'src/app/shared/notification.service';
import { MatDialog } from '@angular/material';

public GetShirtCodes = (): Observable<any> =>  
{  
    return this._http.get(this.actionUrl)  
        pipe.(map((response: Response) => <any>response.json())); //shows error on response
}

但我相信这是一个过时的解决方案。

【问题讨论】:

  • 需要在ts文件中声明一个变量shirts:any,并在模块的ngOnInt中赋值为shirts = GetShirtCodes()

标签: c# asp.net-web-api angular7


【解决方案1】:

您的 Component.html 必须这样修复:

<mat-form-field style="width:inherit;">
  <mat-label>Shirt Code</mat-label>
    <mat-select>
      <mat-option *ngFor="let shirt of shirts" [value]="shirt.ShirtID">
        {{shirt.ShirtName}}
      </mat-option>
     </mat-select>
 </mat-form-field>

必须在您的模型中设置服务结果:

一般得到:

  public Get<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
    return this.http.get<T>(this.api + endPoint, options);
  }

不太一般地在服务之上调用:

  getAll(endPoint: string): Observable<any> {
    return this.httpClient
      .Get(endPoint)
      .pipe(map(response => response));
  }

终于在你的组件中:

ngOnInit() {
this.service.getAll(endPoint).subscribe(res => {this.shirts = res});
} 

【讨论】:

    【解决方案2】:

    作为初学者不要使用Observables,试试这个

    this._someService.SearchSomeData(this._someId).
    subscribe(x =>
    {
        this.someInfo =x;
    });        
    

    您的someService 方法可能如下所示:

    @Injectable()
    export class CoursesService {
    
    constructor(private _http: HttpClient) { }
    
    SearchSomeData(id, ){
        return this._http.get("/api/Search/GetSomeData/"+id );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多