简体   繁体   English

如何从Angular读取数据到Asp.net Core

[英]How to Read Data come from Angular to Asp.net Core

I have another query 我还有另一个查询

I don't fully understand how to pass Data that comes from angular to asp.net core web api. 我不完全了解如何将来自angular的数据传递给asp.net核心Web API。

This is the Html code of the angular 这是角度的HTML代码

<form [formGroup]="form" (ngSubmit)="onSubmit()">
    <!-- <input formControlName="first" [(ngModel)]="value"> -->
    <mat-form-field>
      <input matInput formControlName="first"  [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="second"   [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>

And this is the .ts code 这是.ts代码

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 {
  selectedDate = new Date();
  form = new FormGroup({
    first: new FormControl(),
    second: new FormControl()
  });

  constructor(private http: HttpClient) { }

  ngOnInit() {
  }

  onSubmit() {
    this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', this.form.value)
    .subscribe(res => {
      console.log(res);
      alert('SUCCESS !!');
    })
  }

}

The angular form was able to call the web api. 角度形式可以调用网络api。

But how can i read the data passed? 但是我如何读取传递的数据? I tried to use the code below to read the contents 我试图使用下面的代码阅读内容

[HttpPost("DataCorrection")]
    public void DataCorrection([FromBody] object data)
    {
        try
        {
            //read the content
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
            throw ex;
        }
    }

I was able to read the data passed. 我能够读取传递的数据。 But using object as the type but when i use a class that has property 但是使用object作为类型但是当我使用具有属性的类时

public class DataCorrectionDto
    {
        public string StartTime { get; set; }
        public string EndTime { get; set; }
    }

The contents is null. 内容为空。

How do i do it properly? 我该怎么做呢? Thank you. 谢谢。

This is because your form names the fields startDate and endDate . 这是因为您的表单将字段命名为startDateendDate While your backend expects the properties StartTime and EndTime . 当您的后端需要属性StartTimeEndTime

Solution one: Rename the form-control-names (frontend) to startTime and endTime . 解决方案一:将form-control-names(前端)重命名为startTimeendTime

Solution two: Rename the properties in your DataCorrectionDto (backend) to StartDate and EndDate 解决方案二:将DataCorrectionDto (后端)中的属性重命名为StartDateEndDate

Solution three: Create a pojo accessing the form fields to get the values 解决方案三:创建一个pojo访问表单字段以获取值

this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', { startTime: this.form.controls['startDate'].value, endTime: this.form.controls['endDate'].value })
    .subscribe(res => {
      console.log(res);
      alert('SUCCESS !!');
    })

If you don't know why this didn't work before, I would recommend to read a little about ModelBinders and how they work in ASP.NET Core . 如果您不知道为什么以前没有ModelBinders ,我建议您阅读一些有关ModelBinders及其在ASP.NET Core中如何工作的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM