Entity framework Navigation Properties

asp.net-mvc entity-framework navigation-properties

Question

I'm now studying the fundamentals as I work to create my first.NET MVC application.

I have a table in my application that shows a list of animals from an animals table. I'm also attempting to display the animal breed in the table, but I'm doing so by retrieving the breed from the Breed table using a foreign key contained in the Animal table.enter image description here

I changed my Animal model so that it now resembles this since I am currently attempting to use a Navigation Property to display the Breed text rather than the ID.

public partial class Animal
{
    public int AnimalId { get; set; }
    public Nullable<int> UserId { get; set; }
    public string TagNo { get; set; }
    public Nullable<int> Species { get; set; }
    public Nullable<bool> Sex { get; set; }
    public Nullable<int> AnimalBreed { get; set; }
    public Nullable<System.DateTime> DOB { get; set; }
    public Nullable<int> OwnershipStatus { get; set; }
    public Nullable<System.DateTime> DateAdded { get; set; }
    public Nullable<bool> BornOnFarm { get; set; }

    public virtual Breed Breed { get; set; }
}

Moreover, my breed model resembles

public partial class Breed
{
    public int id { get; set; }
    public Nullable<int> SpeciesID { get; set; }
    public string Breed1 { get; set; }
}

The Breeds field from my animal model is what I'm trying to display in my view, as shown below, but the breed column is just empty.

<td>
  @Html.DisplayFor(modelItem => item.Breed.Breed1)
</td>

The code I'm using to transmit the model to the view is included here as well, as a final note.

List<Animal> Animal1 = (from animals in db.Animals
    where animals.Species == 2 && animals.OwnershipStatus == 1
    && animals.UserId == WebSecurity.CurrentUserId
    select animals).ToList();

return View(Animal1);
1
1
12/11/2014 11:29:36 PM

Popular Answer

Don't pluralize single items, to start. It causes ambiguity in your code:

public virtual Breed Breed { get; set; }

-or-

public virtual ICollection<Breed> Breeds { get; set; }

The virtual enables for lazy loading (a query to fetch the breed will be issued the first time you try to access it). Almost always, you want to addvirtual if you don't actually use it, with the property so Entity Framework doesn't needlessly issue joins. However, in this instance, you'll need to instruct EF to eager-load it by including.Include("Breed") in the question. But this isn't your issue; that's just for optimization.

The issue you have here is that Razor is unable to presentBreed . Because you developed it, it is evident that it is not a typical type. Therefore, what you actually need is to show the property onBreed that you desire:

@Html.DisplayFor(m => m.Breed.Breed1)

There is an alternative approach, but it is more involved and possibly unnecessary in this situation. If you're determined to useBreed immediately after that, you must define a display template forBreed . By including a new folder to theViews\Shared named DisplayTemplates . Add a view with the name of the folder.Breed.cshtml . The property name, not the class name, is what this view's name refers to. Within such perspective, you might:

@model Namespace.To.Breed
@Html.DisplayFor(m => m.Breed1)

If so, in your opinion, you could simply:

@Html.DisplayFor(m => m.Breed)

And Razor will render the proper thing using the display template. Like I stated, it's overkill for this, but it might be useful in rendering objects that are more intricate.

4
2/28/2014 4:00:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow