1

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.

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

For example I have this:

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

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

enter image description here

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.

I tried this:

 $.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.

enter image description here

What I am doing wrong?

This is what console.log(JSON.stringify(detalleVenta)) shows:

enter image description here

0

1 Answer 1

2

In Doc you can see:

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.

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

Action:

public JsonResult GuardarVenta([FromBody]List<DetalleBinding> detalle,int age,string name)
{
  ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

What if I want to send 2 lists to parameter? Will it work inside Venta/GuardarVenta/? <LIST HERE>, Well I am actually trying to send 2 lists and normal data to controller.
You can create a ViewMdel to wrap two classes in one class, and then accept the ViewModel.
Oh, does ajax accept viewModel? Never tried that but... Do I have to name the same json name to my property? for example in ViewModel public List<class> mylist; and in ajax like this data : {"mylist" : list, .. and so on}, and in method public JsonResult Save (ViewModel hi), right?
Not like that,you need to add it to your array,and then send it just like data: JSON.stringify(detalleVenta)
Oh but how could I send to array inside data: , I tried like this data : JSON.stringify(detalleVenta1, detalleVenta2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.