ASP.NET Core MVC Ajax无法正常工作 [英] ASP.NET Core MVC Ajax not working properly

查看:94
本文介绍了ASP.NET Core MVC Ajax无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My issue is very very simple but I have not found any solution about this issue in the entire internet. I am new at ASP.NET CORE MVC and I feel its AJAXs is very hard to understand compared to the normal ASP.NET.

好吧,我试图将对象的数组/列表传递给控制器​​,但是我做不到.这很简单,但是却无法按照我想要的方式工作(在正常的ASP.NET MVC中效果很好).

Well I am trying to pass an array/list of objects to controller but I cant make this. It is Very simple but it does not work the way I want ( In the normal ASP.NET MVC worked great).

例如,我有这个:

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成功将列表数据发送到控制器:

This way worked for me, ajax sends the list successfully data to controller:

但是我的问题是...如果我想从该数据中发送更多数据,该怎么办?我通常以JSON格式编写它,如下所示: data:{:detalleVenta,名称";:玛丽",年龄":34} ,但是我的问题是这对我不起作用.

But my issue is... what if I want to send more data appart from that data? I Normally write it in JSON format like this : data : { "detalle" : detalleVenta, "Name" : "Mary", "Age" : 34} but my issue is that this does not work for me.

我尝试过:

 $.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")'          
        }
    }
        ,
        })

但是它没有绑定到控制器,并且给了我空值.

But it does not bind to controller and gives me null.

我做错了什么?

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

推荐答案

对于每种操作方法,请勿将[FromBody]应用于多个参数.输入格式器读取请求流后,将不再可以再次读取该流以绑定其他[FromBody]参数.

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

So if you want to send more data ,the easiest way is through querystring,like below(change 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)
{
  ...
}

这篇关于ASP.NET Core MVC Ajax无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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