0

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

enter image description here

{"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.

3
  • The AngularJs tag is for Angular 1.X Commented Apr 3, 2019 at 4:50
  • add the response json you are getting from backend so that it is easy to understand your requirement properly. Commented Apr 3, 2019 at 5:14
  • @ganesh045 hello. I updated the question. I don't understand the json response. So what i did is i pasted the response im getting from the web api. I read the response using the Chrome web browser Commented Apr 3, 2019 at 5:32

3 Answers 3

2

I don't think this was the problem,

there is spell mistake in your template, pd.ActualLogin but actually in JSON response it is spelled as actualLogin. Change your template to

   <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>
Sign up to request clarification or add additional context in comments.

Comments

0

Save the result in some variable

example

this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', this.form.value)
    .toPromise()
    .then(res => {
      result = res;
      console.log('Res ' + res);
    });

And try displaying in UI

example

<div>{{result|json}}</div>

This will display result in json format

Note: use ngFor to display result in table based of your output format

3 Comments

He will be getting a cors error because the api is on a different port than the Angular app. This wont help him at all.
The cors is enabled in my application. I updated the question. Kindly please assist me. Thank you
@AdrianBrand when I answered the question, he didn't mentioned about CORS
0

You are doing a cross domain request because your api and Angular app are being served on different ports. You can either add a cors header to accept all domains on your api (not recommended) or you can setup a proxy.config.json file so that your api calls are redirected to the same port as your Angular app.

{
  "/api/*": {
    "target": "http://localhost:5000/api/",
    "pathRewrite": {
      "^/api": ""
    },
    "changeOrigin": true,
    "secure": false,
    "logLevel": "debug"
  }
}

And in your Angular use /api/DataCorrection/DataCorrection instead of http://localhost:5000/api/DataCorrection/DataCorrection

Then serve with

ng serve --proxy-config proxy.config.json

1 Comment

The cors is enabled in my application. But how do i read the data that comes from my asp.net core? I updated my question. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.