簡體   English   中英

ASP.NET 核心 MVC Ajax 無法正常工作

[英]ASP.NET Core MVC Ajax not working properly

我的問題非常簡單,但我在整個互聯網上都沒有找到任何關於這個問題的解決方案。 我是 ASP.NET CORE MVC 的新手,與普通的 ASP.NET 相比,我覺得它的 AJAX 很難理解。

好吧,我正在嘗試將對象數組/列表傳遞給 controller 但我做不到。 它非常簡單,但不能按我想要的方式工作(在正常的 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 controller:

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 成功將列表數據發送到 controller:

在此處輸入圖像描述

但我的問題是......如果我想從該數據中發送更多數據怎么辦? 我通常以 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")'          
        }
    }
        ,
        })

但它不綁定到 controller 並給我 null。

在此處輸入圖像描述

我做錯了什么?

這就是console.log(JSON.stringify(detalleVenta))顯示的內容:

在此處輸入圖像描述

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)
{
  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM