Help - I can not send object of the view towards the controller arrive null attributes

2

I have a problem

I try to send an object in javascript to my controller, the problem is that the controller receives as a list attribute and this list contains another list as an attribute.

I think the problem is that I do not send or do not create my javascript object well.

someone could help me tell me if my mistake is creating the object incorrectly or if it is somewhere else

This is the object I did.

   var retrabajoPiezas = {
      claveSubMenu: "",
      claveContenedorOrigen: "",
      claveContenedorDestino: "",
      Lotes: [{ Defectos: [{ Clave: "", DefectoId: 0, Nombre: "" }] }],
      UsuarioId: 1,
   };

I send the object in this Ajax.

      $.ajax({
        type: 'POST',
        url: "http://localhost/api/Contenedores/RetrabajoPiezas",
        dataType: "json",
        data: retrabajoPiezas,
        traditional: true,
        success: function (datos) {

        },
        error: function () {
            ErrorBottom("JS: Error de servidor al mover piezas.");
        }
    });

This receives the method in C #

  public HttpResponseMessage RetrabajoPiezas(RetrabajoPiezas retrabajoPiezas) 
  {
     ObjectParameter outputMensaje = VariablesEstaticas.outputmensajeError; 
   }

This is the class of the object that I receive in my method

  public class RetrabajoPiezas
  {
    public string ClaveSubMenu { get; set; }
    public string ClaveContenedorOrigen { get; set; }
    public string ClaveContenedorDestino { get; set; }
    public List<LotesClasificacionMaquinadoPiezasDTO> Lotes { get; set; }
    public int UsuarioId { get; set; }
  }

this is the second class (the list found in the previous class)

  public class LotesClasificacionMaquinadoPiezasDTO : LotesDefectosDTO
 {
    public LotesClasificacionMaquinadoPiezasDTO()
    {
       Defectos = new List<ReporteDefectoDTO>();
    }
    public List<ReporteDefectoDTO> Defectos { get; set; }
  }

and here is the class that contains the previous list

 public class  ReporteDefectoDTO
{
   public int DefectoId { get; set; }
   public string Clave { get; set; }
   public string Nombre { get; set; }
 }

as you can see if I get a Batch but the list of Defects is not

this is a console.log of the object that I sent in ajax

    
asked by JehTron 25.10.2018 в 18:58
source

1 answer

1

You can try passing the data as Json and adding the content-type:

var retrabajoPiezasJson = JSON.stringify(retrabajoPiezas);

$.ajax({
        type: 'POST',
        url: "http://localhost/api/Contenedores/RetrabajoPiezas",
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: retrabajoPiezasJson,
        traditional: true,
        success: function (datos) {

        },
        error: function () {
            ErrorBottom("JS: Error de servidor al mover piezas.");
        }
    });

Greetings!

    
answered by 25.10.2018 в 20:10