【发布时间】: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