【问题标题】:ASP.NET Core MVC Ajax not working properlyASP.NET Core MVC Ajax 无法正常工作
【发布时间】:2020-12-02 09:49:49
【问题描述】:

我的问题非常非常简单,但是我在整个互联网上都没有找到任何关于这个问题的解决方案。我是 ASP.NET CORE MVC 的新手,与普通的 ASP.NET 相比,我觉得它的 AJAX 很难理解。

好吧,我正在尝试将对象数组/列表传递给控制器​​,但我无法做到这一点。它非常简单,但不能按我想要的方式工作(在普通的 ASP.NET MVC 中效果很好)。

例如我有这个:

var detalleVenta = new Array();

     $.each($("#tableventa tbody tr"), function () {

                 detalleVenta.push( 
                     {
                         "ProductoId" :  $(this).find("td").eq(6).html(),
                         "Cantidad" :  $(this).find("td").eq(2).html(),
                         "Precio" :  $(this).find("td").eq(3).html(),
                         "Total" :  $(this).find("td").eq(4).html() 
                     }
                     ); 
            });

        $.ajax({
          method:"POST",
          data: JSON.stringify(detalleVenta),
          "dataType": "json",
          "contentType": "application/json",
           url: '@Url.Action("GuardarVenta", "Venta")',
           traditional: true,         
           success: function(data, textStatus) { 
           if (data == "OK" ){
           location.href = '@Url.Action("Index","Compra")'          
        }
    }
        ,
        })

MVC 控制器:

public JsonResult GuardarVenta([FromBody]List<DetalleBinding> detalle)
{
      ...
}

public class DetalleBinding
{
    public string ProductoId {get; set;}
    public string Cantidad {get;set;}
    public string Precio {get;set;}
    public string Total {get;set;}
}

这种方式对我有用,ajax 成功将列表数据发送到控制器:

但我的问题是……如果我想从该数据中发送更多数据怎么办?我通常用 JSON 格式写成这样:data : { "detalle" : detalleVenta, "Name" : "Mary", "Age" : 34} 但我的问题是这对我不起作用。

我试过这个:

 $.ajax({
    method:"POST",
    data: {"detalle" : JSON.stringify(detalleVenta)},
    "dataType": "json",
    "contentType": "application/json",
    url: '@Url.Action("GuardarVenta", "Venta")',
    traditional: true,         
    success: function(data, textStatus) { 
    if (data == "OK" ){
        location.href = '@Url.Action("Index","Compra")'          
        }
    }
        ,
        })

但它没有绑定到控制器并给我 null。

我做错了什么?

这是console.log(JSON.stringify(detalleVenta)) 显示的内容:

【问题讨论】:

    标签: javascript c# ajax asp.net-core-mvc asp.net-ajax


    【解决方案1】:

    在Doc你可以看到:

    Don't apply [FromBody] to more than one parameter per action method. Once the request stream is read by an input formatter, it's no longer available to be read again for binding other [FromBody] parameters.

    所以如果你想发送更多数据,最简单的方法是通过查询字符串,如下所示(更改 url):

    $.ajax({
          method:"POST",
          data: JSON.stringify(detalleVenta),
          dataType: "json",
          contentType: "application/json",
          url: "Venta/GuardarVenta?age=34&name=Marry",
          traditional: true,         
          success: function(data, textStatus) { 
          if (data == "OK" ){
          location.href = '@Url.Action("Index","Compra")'          
        }
    

    行动:

    public JsonResult GuardarVenta([FromBody]List<DetalleBinding> detalle,int age,string name)
    {
      ...
    }
    

    【讨论】:

    • 如果我想向参数发送 2 个列表怎么办?它会在Venta/GuardarVenta/? &lt;LIST HERE&gt; 内工作吗?我实际上是在尝试向控制器发送 2 个列表和普通数据。
    • 您可以创建一个 ViewMdel 将两个类包装在一个类中,然后接受 ViewModel。
    • 哦,ajax 接受 viewModel 吗?从未尝试过,但是...我是否必须为我的财产命名相同的 json 名称?例如在 ViewModel public List&lt;class&gt; mylist; 和像 data : {"mylist" : list, .. and so on} 这样的 ajax 中,以及在方法 public JsonResult Save (ViewModel hi) 中,对吗?
    • 不是这样的,你需要把它添加到你的数组中,然后像data: JSON.stringify(detalleVenta)一样发送它
    • 哦,但是我怎么能发送到data: 里面的数组,我试过这样data : JSON.stringify(detalleVenta1, detalleVenta2)
    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多