How to get a list of JS objects in Controller C# MVC?

Asked

Viewed 340 times

1

I have an object with a list that will return me 3 or more items. I pass this object like this:

 var jsonObjs = JSON.stringify(arrayObj);     
  alert(jsonObjs);      
  CarregaNotas(jsonObjs);

inserir a descrição da imagem aqui

function CarregaNotas(notas) {
           jQuery.ajax({
                   type: "POST",
                    url: "/Expedicao/Minuta/GeraNotas",
                    dataType: "json",
                    data: notas,
                    success: function (data) {


                        }

                    });
                }
            });
        }

How to take this list in the controller and pass or scan in the Model ? For every item I have to add an "And" or "In" to "Select"

1 answer

1


First you create a class with the same properties as the one you are passing in JSON.

public class Obj
{
    public int Quantidade { get; set; }
    public string Doc { get; set; }
    public string Serie { get; set; }
}

In the controller you get the stringJson and converts to a list of this object.

var listaConvertida = JsonConvert.DeserializeObject<List<Obj>>(stringJson);

To use the class JsonConvert it is necessary to have the Nuget package Newtonsoft.Json installed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.