C#中的控制器不会通过邮政从Angular接收参数
html发送以下方式:
<form [formGroup]="Denuncia" (ngSubmit)="guardarDatos()">
<div class="form-group offset-md-1">
<label>(*) Tipo de Denuncia </label>
<select class="combobox" id="Tipo_Denuncia" formControlName="Tipo_Denuncia">
<option value="">--Seleccione Opción--</option>
<option [ngValue]="TipoDenuncia.cod_Tipo_Denuncia" *ngFor="let TipoDenuncia of
TiposDenuncia">{{TipoDenuncia.nombre}}</option>
</select>
</div>
<input [disabled]="!Denuncia.valid" type="submit" value="Generar" class="btn btn-
success col-md-1 offset-md-1" />
</div>
方法上
constructor(private denunciaservice: DenunciaService, private router: Router,
private modalService: NgbModal, private formBuilder: FormBuilder) {
this.Denuncia = this.formBuilder.group(
{
"Tipo_Denuncia": new FormControl("", [Validators.required]),
}
);
}
调用服务
guardarDatos() {
if (this.Denuncia.valid == true) {
this.denunciaservice.agregarDenuncia(this.Denuncia.value).subscribe(data => {
if (data) {
console.log(data);
this.resultadoGuardadoModal = "ok";
}
else
this.resultadoGuardadoModal = "Not Ok!";
});
}
}
服务。
public agregarDenuncia(Denuncia: any): Observable<any>
{
var url = this.urlBase + 'api/Denuncia/guardarDenuncia/';
return this.http.post(url,Denuncia);
}
在
从组件
[HttpPost]
[Route("api/Denuncia/guardarDenuncia")]
public int guardarDenuncia([FromBody] DenunciaDTO oDenunciaDTO)
{
int rpta = 0;
try
{
using (M_VPSA_V3Context bd = new M_VPSA_V3Context())
{
using (var transaccion = new TransactionScope())
{
oDenuncia.CodTipoDenuncia = int.Parse(oDenunciaCLS2.Tipo_Denuncia!);
}
}
rpta = 1;
}
catch (Exception ex)
{
Console.WriteLine(ex);
rpta = 0;
}
return rpta;
}
component.ts 发生了什么??
Form from Component HTML send this:
<form [formGroup]="Denuncia" (ngSubmit)="guardarDatos()">
<div class="form-group offset-md-1">
<label>(*) Tipo de Denuncia </label>
<select class="combobox" id="Tipo_Denuncia" formControlName="Tipo_Denuncia">
<option value="">--Seleccione Opción--</option>
<option [ngValue]="TipoDenuncia.cod_Tipo_Denuncia" *ngFor="let TipoDenuncia of
TiposDenuncia">{{TipoDenuncia.nombre}}</option>
</select>
</div>
<input [disabled]="!Denuncia.valid" type="submit" value="Generar" class="btn btn-
success col-md-1 offset-md-1" />
</div>
On Component.ts
constructor(private denunciaservice: DenunciaService, private router: Router,
private modalService: NgbModal, private formBuilder: FormBuilder) {
this.Denuncia = this.formBuilder.group(
{
"Tipo_Denuncia": new FormControl("", [Validators.required]),
}
);
}
Method call service
guardarDatos() {
if (this.Denuncia.valid == true) {
this.denunciaservice.agregarDenuncia(this.Denuncia.value).subscribe(data => {
if (data) {
console.log(data);
this.resultadoGuardadoModal = "ok";
}
else
this.resultadoGuardadoModal = "Not Ok!";
});
}
}
Service.ts:
public agregarDenuncia(Denuncia: any): Observable<any>
{
var url = this.urlBase + 'api/Denuncia/guardarDenuncia/';
return this.http.post(url,Denuncia);
}
And declare class DenunciaDTO
Finally on the controller i dont recieve the parameter on the object DenunciaDTO
[HttpPost]
[Route("api/Denuncia/guardarDenuncia")]
public int guardarDenuncia([FromBody] DenunciaDTO oDenunciaDTO)
{
int rpta = 0;
try
{
using (M_VPSA_V3Context bd = new M_VPSA_V3Context())
{
using (var transaccion = new TransactionScope())
{
oDenuncia.CodTipoDenuncia = int.Parse(oDenunciaCLS2.Tipo_Denuncia!);
}
}
rpta = 1;
}
catch (Exception ex)
{
Console.WriteLine(ex);
rpta = 0;
}
return rpta;
}
When execute this oDenunciaDTO is null on the controller. What Happened??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
[从Body]
到[From Form]
,您的问题将得到解决。Change
[FromBody]
to[FromForm]
, and your issue will be fixed.