ASP.Net MVC 5 Scaffolding Template Does not show the related property in the View Details

1

Create the Book Model:

Then the Model Matter

Then I created the Controlers and assigned them the Models and generated the Views, here in the View Details.cshtml, when I navigate from the list of Books records through the Detail link, it does not show me the value of the attribute Matter

Is there something I'm not seeing? How can I show the value of the attribute in the details view?

Edited: I add in text the Controller code of the Book class:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using InventarioLibros.Models;

namespace InventarioLibros.Controllers
{
public class LibrosController : Controller
{
    private InventarioLibrosContext db = new InventarioLibrosContext();

    // GET: Libros
    public ActionResult Index()
    {
        var libros = db.Libros.Include(l => l.MateriaLibro).Include(l => l.UbicacionLibro);
        return View(libros.ToList());
    }

    // GET: Libros/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Libro libro = db.Libros.Find(id);
        if (libro == null)
        {
            return HttpNotFound();
        }
        return View(libro);
    }

    // GET: Libros/Create
    public ActionResult Create()
    {
        ViewBag.MateriaId = new SelectList(db.Materias, "MateriaId", "Nombre");
        ViewBag.UbicacionId = new SelectList(db.Ubicaciones, "UbicacionId", "Nombre");
        return View();
    }

    // POST: Libros/Create
    // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
    // más información vea https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "LibroId,Titulo,Autor,Año,Paginas,MateriaId,UbicacionId")] Libro libro)
    {
        if (ModelState.IsValid)
        {
            db.Libros.Add(libro);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.MateriaId = new SelectList(db.Materias, "MateriaId", "Nombre", libro.MateriaId);
        ViewBag.UbicacionId = new SelectList(db.Ubicaciones, "UbicacionId", "Nombre", libro.UbicacionId);
        return View(libro);
    }

    // GET: Libros/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Libro libro = db.Libros.Find(id);
        if (libro == null)
        {
            return HttpNotFound();
        }
        ViewBag.MateriaId = new SelectList(db.Materias, "MateriaId", "Nombre", libro.MateriaId);
        ViewBag.UbicacionId = new SelectList(db.Ubicaciones, "UbicacionId", "Nombre", libro.UbicacionId);
        return View(libro);
    }

    // POST: Libros/Edit/5
    // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
    // más información vea https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "LibroId,Titulo,Autor,Año,Paginas,MateriaId,UbicacionId")] Libro libro)
    {
        if (ModelState.IsValid)
        {
            db.Entry(libro).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.MateriaId = new SelectList(db.Materias, "MateriaId", "Nombre", libro.MateriaId);
        ViewBag.UbicacionId = new SelectList(db.Ubicaciones, "UbicacionId", "Nombre", libro.UbicacionId);
        return View(libro);
    }

    // GET: Libros/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Libro libro = db.Libros.Find(id);
        if (libro == null)
        {
            return HttpNotFound();
        }
        return View(libro);
    }

    // POST: Libros/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Libro libro = db.Libros.Find(id);
        db.Libros.Remove(libro);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

}

    
asked by Christian Cuadros Betancur 19.12.2017 в 21:57
source

2 answers

0

the declaration of the virtual modifier in the MateriaBook attribute on line 20 of the Book class is what I needed for the EF to use its Lazy Loading to load the property

    
answered by 04.01.2018 / 14:08
source
0

With the virtual switch in the MateriaLibro attribute it works perfectly, you can also use another way, modifying your LINQ query in the example driver:

  // GET: /Libros/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var libro = db.Libros.Include(l=>l.MateriaLibro).FirstOrDefault(l=>l.LibroId == id);
            if (libro == null)
            {
                return HttpNotFound();
            }
            return View(libro);
        }

Using Include EF load the data of the Matter, this way you can take it into account since I tried to make your example in an App Asp.Net Core 2 and it does not work, nevertheless this way if it works in all the cases . I hope you find it useful

    
answered by 20.12.2017 в 18:53