C#中的控制器不会通过邮政从Angular接收参数

发布于 2025-01-29 09:06:20 字数 2553 浏览 2 评论 0原文

html发送以下方式:

 <form [formGroup]="Denuncia" (ngSubmit)="guardarDatos()">
 <div class="form-group offset-md-1">
 <label>(*) Tipo de Denuncia&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2025-02-05 09:06:20

更改[从Body][From Form],您的问题将得到解决。

Change [FromBody] to [FromForm], and your issue will be fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文