【问题标题】:How to Read Data come from Angular to Asp.net Core如何从 Angular 读取数据到 Asp.net Core
【发布时间】:2019-08-23 12:42:21
【问题描述】:

我还有一个问题

我不完全了解如何将来自 angular 的数据传递给 asp.net core web api。

这是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>

这是.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 !!');
    })
  }

}

angular形式可以调用web api。

但是我怎样才能读取传递的数据呢?我尝试使用下面的代码来阅读内容

[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;
        }
    }

我能够读取传递的数据。但是使用object 作为类型但是当我使用具有属性的类时

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

内容为空。

我该如何正确地做到这一点?谢谢。

【问题讨论】:

    标签: angular asp.net-core


    【解决方案1】:

    这是因为您的表单将字段命名为 startDateendDate。而您的后端需要属性 StartTimeEndTime

    解决方案一: 将表单控制名称(前端)重命名为 startTimeendTime

    解决方案二: 将DataCorrectionDto(后端)中的属性重命名为StartDateEndDate

    解决方案三: 创建一个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 !!');
        })
    

    如果您之前不知道为什么这不起作用,我建议您阅读一些关于 ModelBinders 以及它们如何在 ASP.NET Core 中工作的内容。

    【讨论】:

    • 它正在工作。我很高兴有堆栈溢出。像你这样的人回答像我这样的新手的问题。非常感谢
    • 很高兴我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2014-05-06
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多