Hello, I'm just starting to learn ASP.NET MVC. I have a problem with displaying the details of the owner, the name is displayed, the last name, but information about the machine is not displayed. I did it on the example from the Microsoft website for learning ASP.NET. public class Owner {

public int OwnerID { get; set; } public string Name { get; set; } public string Surname { get; set; } public DateTime Birthday { get; set; } public int Experience { get; set; } public virtual ICollection<CarOwner> CarOwners { get; set; } } public class Car { public int CarID { get; set; } public string Model { get; set; } public string Mark { get; set; } public string Type { get; set; } public int Price { get; set; } public string YearofRelease { get; set; } public virtual ICollection<CarOwner> CarOwners { get; set; } } public class CarOwner { public int CarOwnerID { get; set; } public int OwnerID { get; set; } public int CarID { get; set; } //public decimal? Grade { get; set; } public virtual Car Car { get; set; } public virtual Owner Owner { get; set; } } public class CarOwnersContext: DbContext { public CarOwnersContext():base("CarOwnersContext") { } public DbSet<Owner> Owners { get; set; } public DbSet<CarOwner> CarOwners { get; set; } public DbSet<Car> Cars { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } public class CarOwnerInit:DropCreateDatabaseIfModelChanges<CarOwnersContext> { protected override void Seed(CarOwnersContext context) { var owners = new List<Owner> { new Owner {Name="Sam", OwnerID=1, Surname="Lucky", Birthday=DateTime.Parse("1993-06-21"), Experience=3}, new Owner { Name="Nick", Surname="May", Birthday=DateTime.Parse("1994-04-06"), Experience=5 } }; owners.ForEach(s => context.Owners.Add(s)); context.SaveChanges(); var cars = new List<Car> { new Car {Model="gghguyg",CarID=2, Mark="ghffgy", Type="gghf", Price=7, YearofRelease="2001" }, new Car {Model="gghguyg",CarID=4, Mark="ghffgy", Type="gghf", Price=77, YearofRelease="2011" } }; cars.ForEach(s => context.Cars.Add(s)); context.SaveChanges(); var carowners = new List<CarOwner> { new CarOwner { OwnerID=1, CarID=2,CarOwnerID=1}, new CarOwner { OwnerID=2, CarID=4,CarOwnerID=2 } }; carowners.ForEach(s => context.CarOwners.Add(s)); context.SaveChanges(); } } Код для отображения подробностей о пользователе @model CarOwners.Models.Owner @{ ViewBag.Title = "Details"; } <h2>Details</h2> <div> <h4>Owner</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Name) </dt> <dd> @Html.DisplayFor(model => model.Name) </dd> <dt> @Html.DisplayNameFor(model => model.Surname) </dt> <dd> @Html.DisplayFor(model => model.Surname) </dd> <dt> @Html.DisplayNameFor(model => model.Birthday) </dt> <dd> @Html.DisplayFor(model => model.Birthday) </dd> <dt> @Html.DisplayNameFor(model => model.Experience) </dt> <dd> @Html.DisplayFor(model => model.Experience) </dd> <dt> @Html.DisplayNameFor(model => model.CarOwners) </dt> <dd> <table class="table"> <tr> <th>Course Title</th> <th>Grade</th> </tr> @foreach (var item in Model.CarOwners) { <tr> <td> @Html.DisplayFor(modelItem => item.Car.Mark) </td> <td> @Html.DisplayFor(modelItem => item.CarOwnerID) </td> </tr> } </table> </dd> </dl> </div> <p> @Html.ActionLink("Edit", "Edit", new { id = Model.OwnerID }) | @Html.ActionLink("Back to List", "Index") </p> 
  • Can you show the Controller code for your View? Run under debugging and see which items are in the CarOwners collection, maybe it is empty? - klutch1991

1 answer 1

Generally speaking, here you need to clarify which version of EF you are using. As I understand it, and the context of the problem, it will be relevant for you to organize a many-to-many relationship between your Car and Owner entities. At the moment, EF can take on the task of creating a so-called intermediate table that will provide this connection ( MM ). Consequently, the CarOwner class is already superfluous. Your models will look something like this:

 public class Owner { public int OwnerID { get; set; } public string Name { get; set; } public string Surname { get; set; } public DateTime Birthday { get; set; } public int Experience { get; set; } public virtual ICollection<Car> Cars { get; set; } } public class Car { public int CarID { get; set; } public string Model { get; set; } public string Mark { get; set; } public string Type { get; set; } public int Price { get; set; } public string YearofRelease { get; set; } public virtual ICollection<Owner> Owners { get; set; } } 

Since collections are marked with the virtual modifier, EF will automatically pull up the necessary objects from other tables. When you receive a collection on a context instance, you will receive ready objects with all the entities nested in them, in accordance with the relations that are in the database. Since you are using CodeFirst, then use Migrations, it will help to monitor the changes that are made to the database as the model classes change.

@ Html.DisplayNameFor (model => model.CarOwners)

The meaning of this line for me personally, to be honest, is not very clear. Is it really written in the example of Microsoft?

In general, after you add model classes and generate a base for them, just add a new MVC controller class, for example:

 public class OwnersController : Controller { // GET: Owners public ActionResult Index(int id = 1) { var c = new CarOwnersContext(); return View(c.Owners.ToList().Single(o => o.OwnerID==id)); } } 

Such a controller will return to View as a model an instance of type Owner with the specified id . Well, in the View itself, display what you want to see about the current owner’s car, for example, like this:

 @Html.DisplayFor(model => model.Car.Type) 
  • When I try to write @ Html.DisplayFor (model => model.Car.Type) in View, I have an error. It does not see Car. When creating the database, I did everything as in the code above, only removed everything related to the CarOwner class. Maybe I am generating the wrong base? - Ksu
  • @Ksu, which version of the studio are you using? What exactly is a mistake? - klutch1991
  • I am using version 13 studio. @ Html.DisplayFor (model => model.Car.Type) in this line writes Car. - Ksu