如何使用ASP Net Core 3.1通过Ajax将对象列表发送到控制器? [英] How to send a list of objects to controller via Ajax using asp net core 3.1?

查看:39
本文介绍了如何使用ASP Net Core 3.1通过Ajax将对象列表发送到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ajax和asp .net core将对象列表发送到控制器.我的问题非常简单,SO中有很多关于我的问题的答案,但是没有一个答案可以解决我的问题.我是ASP.NET CORE的新手,但在ASP.NET(正常情况下)方面经验丰富.

I am trying to send a list of objects to controller using ajax and asp .net core. My issue is very very simple and there are lots of answers in SO about my issue but none of them solved my issue. I am new at ASP.NET CORE but very experienced at ASP.NET( the normal one).

调试时,我得到对象的 null 列表.

When debbuging I get a null list of object.

这是我的代码:

var detalleVenta = new Array();
    detalleVenta.length = 0;


     $.each($("#tableventa tbody tr"), function () {
                detalleVenta.push({
                    ProductoId: $(this).find('td:eq(6)').html(),
                });
            });

console.log(detalleVenta);

  var datos =
{  
    // ignore this, i am trying to capture other data.
    "Nit": document.getElementById('Nit').value,
    "Nombres":document.getElementById('nombres').value,
    "NoComprobante":document.getElementById('nocomprobante').value,
    "ClienteId":document.getElementById('clienteselect').value,
    "Direccion":document.getElementById('direccion').value,
    "FormaPago":document.getElementById('selectformapago').value,
    

    // This is the list I want to send to controller
    "detalle": detalleVenta,
};

  $.ajax(
    {     
        url: '/Venta/GuardarVenta/',        
        method:"POST",
        data: datos,
        traditional: true,      
        success: function(data, state) { 
            location.reload();
            return;
        }, 
        error: function(xhr, textStatus, txt) {
            alert(xhr.responseText);
            return;
        }

    });

console.log(detalleVenta); 代码中,我得到了:

但是当点击参数控制器时,我得到了:

but when hitting parameters controller, I get this:

这是我的C#代码:

public class VentaController : Controller
{
   [HttpPost]
   public JsonResult GuardarVenta(List<Binderr> detalle, Venta venta)
   {
   }
}
public class Binderr
{
    public string ProductoId {get;set;}
}

参数 Venta venta 可以很好地捕获参数,但列表 deletle 却不能.我在做什么错了?

The parameter Venta venta captures good the parameters but the list detalle does not. What am I doing wrong?

尝试使用 public JsonResult GuardarVenta(Binderr [] detalle,Venta venta 得到相同的结果,控制器中的列表为空.

tried public JsonResult GuardarVenta(Binderr[] detalle, Venta venta got same results, list is null in the controller.

推荐答案

通常,最简单的方法是以 Json 的形式将数组传递给后端.

Usually the easiest way is to pass the array to the backend in the form of Json.

这是一个简单的例子:

 var detalleVenta =
            [
            { id: "1" },
            { id: "2" },
            { id: "3" }
            ]
       
        $.ajax({
            "type": "POST",
            "url": '/Venta/GuardarVenta/',
            "dataType": "json",
            "contentType": "application/json",
            "data": JSON.stringify(detalleVenta),
            })

Controller(添加[FromBody] ):

Controller(Add [FromBody]):

[HttpPost]
public JsonResult GuardarVenta([FromBody]List<Binderr> detalle)
{
}

结果:

这篇关于如何使用ASP Net Core 3.1通过Ajax将对象列表发送到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆