简体   繁体   English

如何使用asp net core 3.1通过Ajax将对象列表发送到controller?

[英]How to send a list of objects to controller via Ajax using asp net core 3.1?

I am trying to send a list of objects to controller using ajax and asp .net core.我正在尝试使用 ajax 和 asp .net 核心将对象列表发送到 controller。 My issue is very very simple and there are lots of answers in SO about my issue but none of them solved my issue.我的问题非常简单,SO 中有很多关于我的问题的答案,但没有一个能解决我的问题。 I am new at ASP.NET CORE but very experienced at ASP.NET( the normal one).我是 ASP.NET CORE 的新手,但在 ASP.NET(普通)方面非常有经验。

When debbuging I get a null list of object.调试时,我得到 object 的null列表。

This is my code:这是我的代码:

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

    });

In the console.log(detalleVenta);console.log(detalleVenta); code I get this:代码我得到这个:

在此处输入图像描述

but when hitting parameters controller, I get this:但是当达到参数 controller 时,我得到了这个:

在此处输入图像描述

Here are my C# code:这是我的 C# 代码:

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

The parameter Venta venta captures good the parameters but the list detalle does not.参数Venta venta捕获了良好的参数,但列表detalle没有。 What am I doing wrong?我究竟做错了什么?

EDIT: tried public JsonResult GuardarVenta(Binderr[] detalle, Venta venta got same results, list is null in the controller.编辑:尝试public JsonResult GuardarVenta(Binderr[] detalle, Venta venta得到了相同的结果,列表是 controller 中的 null。

在此处输入图像描述

Usually the easiest way is to pass the array to the backend in the form of Json .通常最简单的方法是以Json的形式将数组传递到后端。

Here is a simple example:这是一个简单的例子:

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

Controller( Add [FromBody] ):控制器( Add [FromBody] ):

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

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP Net Core3.1 AJAX 后发送 NULL 到 Controller - ASP Net Core3.1 AJAX Post Sending NULL to Controller 如何使用jQuery和Ajax将动态对象列表发送到MVC中的控制器 - How to send a dynamic list of objects to controller in MVC using jQuery and Ajax ASP .NET Core MVC JQuery AJAX - problem with getting data to view via controller api using ajax - ASP .NET Core MVC JQuery AJAX - problem with getting data to view via controller api using ajax 使用Ajax将多个对象发送到ASP.NET MVC控制器 - Send Multiple objects with Ajax to the ASP.NET MVC controller Ajax .NET Core MVC send JSON Array to Controller using Ajax - Ajax .NET Core MVC send JSON Array to Controller using Ajax 如何将base64String转换为blob并通过ASP.NET MVC中的ajax发送到controller? - How to convert base64String to blob and send to controller via ajax in ASP.NET MVC? 无法在asp.net核心中使用ajax将json发布到控制器 - Cannot post json to controller using ajax in asp.net core 需要使用 ajax 帖子将对象列表和其他 object 发送到 controller - Need to send list of objects and other object to controller using ajax post 如何从 AJAX 上传文件到 asp.net 核心控制器 - How to upload file from AJAX to asp.net core controller 如何在 asp .net Core 中将列表列表从视图发布到控制器? - How to post List of List from view to Controller in asp .net Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM