Why does not the Controller read the Detail list of a Json file for Master Detail?

0

I am developing a master form with details from a JSON in the browser to the server. The Master part of the json file is read and saved in the database but the controller does not read the details part (a list) (in bold the line of code it does not interpret) (foreach (var x in reference. container_references)) of the json file passed as a reference parameter.

Master Model:

  public partial class referencia
  {
     [System.Diagnostics.CodeAnalysis.SuppressMessage    

    ("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public referencia()
    {
    referenciascontenedores = new HashSet<referenciascontenedore>();
    }

    [Key]
    [Display(Name = "Id")]
    public int ID { get; set; }

    ........

    [System.Diagnostics.CodeAnalysis.SuppressMessage  
    ("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]

    public virtual ICollection<referenciascontenedore> 
    referenciascontenedores { get; set; }

    }

Detail Model:

    public class referenciascontenedore
    {
       [Key]
       [Display(Name = "Contenedores Id")]
       public int ref_num { get; set; }

     [ForeignKey("referencia")]        
     [Display(Name = "Referencia Id")]
      public int referenciaID { get; set; }

     .......
     [Required]
      public virtual referencia referencia { get; set; }
   }

View Model:

   public class referenciaViewModel
   {
        [StringLength(150)]
        [Display(Name = "Referencia")]
        public string ref_referencia { get; set; }
        ........

        public List<referenciascontenedore> referenciascontenedores { get; 
        set; } = 
        new List<referenciascontenedore>();       
   }

Controller:

   [HttpPost]
   [ValidateAntiForgeryToken]
   public JsonResult Create(referenciaViewModel reference)
   {
      bool status = false;
      if (ModelState.IsValid)
   {
      referencia referencia = new referencia {ref_referencia =          
      reference.ref_referencia, ... };

      foreach (var x in reference.referenciascontenedores)**texto en   negrita**
   {
       referencia.referenciascontenedores.Add(x);
   }
   db.referencias.Add(referencia);                
   db.SaveChanges();
   status = true;
   }
   else
   {
        status = false;
   }
   return new JsonResult { Data = new { status = status } };

The Javascripfile:

    var referenciadetalle = 
    {
    ref_tipocontenedor: $('.select', this).val(),
    .....
    }
    list.push(referenciadetalle);

    if (isAllValid) {
    var data = {               
   ref_referencia: $('#ref_referencia').val(),
   ........,
   referenciascontenedores: list
   }

  data = JSON.stringify(data);
  alert(data);
   $.ajax({
       type: 'POST',
       url: '/Referencias/Create',
       //data: JSON.stringify(data),
       data : data,
       contentType: 'application/json',
       dataType: 'json',               
       success: function (data) {
           alert("Guardando");
           if (data.status) {
               alert('Guardados correctamente');
               //here we will clear the form
               list = [];
               $('#referenciadetallescontenedores').empty();
               window.location.href = "/Referencias/Index";
           }
           else {
               alert('Error');
           }
           $('#guardardetalles').val('Guardar referencia y  
            contenedores');
       },
       error: function (error) {
           console.log(error);
           $('#guardardetalles').val('Guardar referencia y 
           contenedores');
       }
     });

Thanks in advance

    
asked by Egb 09.01.2018 в 01:08
source

0 answers