As I add a list in a relationship many to many entity MVC5

0

I try to add a list of subCategories from a book to a book the models:

 public partial class Libro : Entidad
    {
        public string Titulo { get; set; }
        public string Autor { get; set; }
        public string ISBN { get; set; }
        public string Descripcion { get; set; }

        [DataType(DataType.Currency)]
        [DisplayFormat(DataFormatString ="{0:C2}", ApplyFormatInEditMode = false)]
        public decimal Precio { get; set; }
        public int? Ano { get; set; }
        public string Editorial { get; set; }
        public string Edicion { get; set; }
        public int Cantidad { get; set; }
        public string UsrCr { get; set; }
        public DateTime? FechaCr { get; set; }

        public virtual ICollection<SubCategoria> SubCategorias { get; set; }
        public virtual ICollection<LibroFoto> LibroFotos { get; set; }
    }

    public class SubCategoria : Entidad
    {
        public string Nombre { get; set; }
        public string Descripcion { get; set; }

        public int? CategoriaId { get; set; }
        public virtual Categoria Categoria { get; set; }

        public virtual ICollection<Libro> Libros { get; set; }
    }

The controller code when I try to create a book:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Id,Titulo,Autor,ISBN,Descripcion,Precio,Ano,Editorial,Edicion,Cantidad,UsrCr,FechaCr")] Libro libro)
    {

        if (ModelState.IsValid)
        {
            var listaSubCategorias = (IEnumerable<SubCategoria>)Session["SubCategoria"];
            libro.LibroFotos = (List<LibroFoto>)Session["Libro"];

            foreach (SubCategoria item in listaSubCategorias)
            {
                libro.SubCategorias.Add(item);
            }

            db.Libros.Add(libro);
            await db.SaveChangesAsync();

            Session["SubCategoria"] = new List<SubCategoria>();
            return RedirectToAction("Index");
        }

        return View(libro);
    }

and the error on the screen:

Thank you .. !!

    
asked by Antonio David Restrepo Carvaja 07.11.2018 в 21:05
source

2 answers

0

Your many-to-many re-election is defined correctly. the problem is that the statement (IEnumerable<SubCategoria>)Session["SubCategoria"]; is returning null. Verify that the value where you keep the sub categories in the Session dictionary is the same one you are using to obtain them ("SubCategory").

For more information: link

    
answered by 08.11.2018 в 19:17
0

Resolved the matter, I had to re-invoice the constructor of the two classes, in the following way: class book:

public partial class Libro : Entidad
{
    public Libro()
    {
        SubCategorias = new List<SubCategoria>();
    }

    public string Titulo { get; set; }
    public string Autor { get; set; }
    public string ISBN { get; set; }
    public string Descripcion { get; set; }

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString ="{0:C2}", ApplyFormatInEditMode = false)]
    public decimal Precio { get; set; }
    public int? Ano { get; set; }
    public string Editorial { get; set; }
    public string Edicion { get; set; }
    public int Cantidad { get; set; }
    public string UsrCr { get; set; }
    public DateTime? FechaCr { get; set; }

    public virtual ICollection<SubCategoria> SubCategorias { get; set; }
    public virtual List<LibroFoto> LibroFotos { get; set; }
}

class Subcategory

public class SubCategoria : Entidad
{
    public SubCategoria()
    {
        Libros = new List<Libro>();
    }
    public string Nombre { get; set; }
    public string Descripcion { get; set; }

    public int? CategoriaId { get; set; }
    public virtual Categoria Categoria { get; set; }

    public virtual ICollection<Libro> Libros { get; set; }
}

In such a way you always have an empty list of the relationship to many. thank you very much friends!

    
answered by 10.11.2018 в 00:32