On the HTML, when you click the exportok() button below, I wish to send the current year (aka.2020) from the selected dropdown to the.NET MVC Controller function called EXPORT via a HTTPGET call "/api/stations/export”.
Admin.component.ts
exportyears = [
{name: '1 July 2019 to 30 June 2020', id: '2019-2020'},
{name: '1 July 2020 to 30 June 2021', id: '2020-2021'},
];
Admin.component.html
<form>
<select formControlName="exportyear" [(ngModel)]="reportyear"(change)="onClickMe()">
<option
*ngFor="let exportyear of exportyears" [ngValue]="exportyears"
>
{{ exportyear.id }}
</option>
</select>
</form>
<button class="btn btn-primary" type="button" (click)="exportOk(exportyears[0].id)">
<i class="glyphicon glyphicon-ok"></i> OK</button>
Service.ts
export(reportyear: string): Observable<Response> {
// let headers = new Headers();
// headers.append('Content-Type', 'application/json');
// headers.append('reportyear', reportyear);
let request = new Request({
method: RequestMethod.Get,
url: environment.baseUrl + "/api/stations/export",
responseType: ResponseContentType.Blob,
body: {reportyear}
}
);
Station.ts
exportOk(reportyear: string) {
this.exporting = true;
this.exportMessage = null;
this.stationsService.export(reportyear)
.subscribe(
(res: Response) => {
this.exporting = false;
this.exportModal.hide();
Utils.launchAttachment(res);
},
error => {
this.exporting = false;
if (error.error) {
this.exportMessage = error.error;
}
else {
this.exportMessage = error;
}
}
);
ASP.NET MVC Controller Controller.cs
[HttpGet("export")]
public async Task<IActionResult> Export([FromBody]string reportyear)
{
var obj = reportyear; //show me the year so I can use it further down the line…..
var stations = await stationRepository.GetExportAsync();
......
}