【发布时间】: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