Good day,
I've been doing some self study in Angular 7 and Asp.net core web api.
And now i want to read the result from asp.net core and show in in Angular 7.
This is the code from Asp.net Core
[HttpPost("DataCorrection")]
public IActionResult DataCorrection([FromBody] DataCorrectionDto data)
{
try
{
var request = Request;
var values = _dataCorrection.GetDateRange(data.StartDate, data.EndDate);
return Ok(values);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw ex;
}
}
The method GetDateRange returns a List of DataCorrection Model
And this is my angular code
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-data-correction',
templateUrl: './data-correction.component.html',
styleUrls: ['./data-correction.component.css']
})
export class DataCorrectionComponent implements OnInit {
list: any[];
selectedDate = new Date();
form = new FormGroup({
StartDate: new FormControl(),
EndDate: new FormControl()
});
constructor(private http: HttpClient) { }
ngOnInit() {
}
onSubmit() {
this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', this.form.value)
.toPromise()
.then(res => {
this.list = res as any[];
console.log('Called');
});
}
}
And this is the Html Code
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<!-- <input formControlName="first" [(ngModel)]="value"> -->
<mat-form-field>
<input matInput formControlName="StartDate" [matDatepicker]="StartDate" placeholder="Start date">
<mat-datepicker-toggle matSuffix [for]="StartDate"></mat-datepicker-toggle>
<mat-datepicker #StartDate ></mat-datepicker>
</mat-form-field>
<mat-form-field>
<input matInput formControlName="EndDate" [matDatepicker]="EndDate" placeholder="End date">
<mat-datepicker-toggle matSuffix [for]="EndDate"></mat-datepicker-toggle>
<mat-datepicker #EndDate ></mat-datepicker>
</mat-form-field>
<div class="form-group">
<button class="btn btn-primary">Submit</button>
</div>
</form>
<tr *ngFor="let pd of list">
<td >{{pd.ActualLogin}}</td>
<td >{{pd.ActualLogout}}</td>
<td >{{pd.ShiftLogin}}</td>
<td>
<i class="far fa-trash-alt fa-lg text-danger" ></i>
</td>
</tr>
And now how can i read it properly so that i can load it in a table.
With the code above. It doesn't even populate the table. But it calls the console.log
I tried searching for a solution for hours now. But i'm lost. I don't understand the tutorials and i can't make it work.
This is the response from my web api. I read it from the response tab in the chrome web browser
{"emp_Id":963,"actualLogin":"05:00:11","actualLogout":"05:01:00","shiftLogin":"05:00:00","shiftLogout":"13:00:00","date":"2019-04-03T00:00:00Z"}
This data is what i want to be populated in the table
Thank you.
