How to display foreign key values in MVC View

13,097

Rewrite your action:

public ActionResult SearchIndex(int networthfrom = 0, int networthto = 0, int landfrom = 0, int landto = 0, int nwafrom = 0, int nwato = 0)
    {
        var provinces = db.Provinces.Include(p=>p.Kingdoms);
        return View(provinces);
    }

and use in view

@Html.DisplayFor(modelItem => item.Kingdoms.Kingdom)
Share:
13,097
MDe
Author by

MDe

Updated on June 25, 2022

Comments

  • MDe
    MDe almost 2 years

    I am trying my best to get a simple MVC application to display foreign key values, but I just can't get it to work.

    Here's my classes:

    public partial class Kingdoms
    {
        public Kingdoms()
        {
            this.Provinces = new HashSet<Provinces>();
        }
    
        [Key]
        public int KingdomID { get; set; }
        public string Kingdom { get; set; }
        public Nullable<int> KingdomNr { get; set; }
        public Nullable<int> IslandNr { get; set; }
        public Nullable<System.DateTime> Created { get; set; }
        public Nullable<System.DateTime> Modified { get; set; }
        public bool AtWar { get; set; }
        public string Stance { get; set; }
    
        public virtual ICollection<Provinces> Provinces { get; set; }
    }
    
    public partial class Provinces
    {
        public Provinces()
        {
            this.ProvinceData = new HashSet<ProvinceData>();
        }
    
        [Key]
        public int ProvinceID { get; set; }
        public int KingdomID { get; set; }
        public string Province { get; set; }
        public string Race { get; set; }
        public Nullable<int> Land { get; set; }
        public Nullable<int> Networth { get; set; }
        public Nullable<System.DateTime> Created { get; set; }
        public Nullable<System.DateTime> Modified { get; set; }
    
        public virtual Kingdoms Kingdoms { get; set; }
        public virtual ICollection<ProvinceData> ProvinceData { get; set; }
    }
    

    This is in my Controller:

    public ActionResult SearchIndex(int networthfrom = 0, int networthto = 0, int landfrom = 0, int landto = 0, int nwafrom = 0, int nwato = 0)
        {
            var provinces = from p in db.Provinces select p;
            return View(provinces);
        }
    

    Now, I have to say that I am using the Add View option in Visual Studio 2012 from this ActionResult. The default View (SearchIndex.cshtml) will show:

    @Html.DisplayFor(modelItem => item.KingdomID)
    

    Which will show the ID of the Kingdom. But I want to show the string value Kingdom from Kingdoms class. I have tried the following:

    @Html.DisplayFor(modelItem => item.Kingdoms.Kingdom)
    

    But that just generates this: Error. An error occurred while processing your request.

    I am publishing this application to Azure FYI.

    • kerzek
      kerzek over 10 years
      Is the error a Null reference exception? If so, it could be because Kingdoms are not loaded (lazy loading). I'd suggest creating a view model, and use it in the view
    • JBeagle
      JBeagle over 10 years
      ViewModels and AutoMapper are your friends. I suggest a good read up on these.