Reputation: 383
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.
Upvotes: 3
Views: 14245
Reputation: 2444
Suppose you want to show all columns of table1HasForeignKey alongside some column of another table named Table2-has-referenced-PrimaryKey . Also table table1HasForeignKey has a foreign key is referencing to the primary key of table Table2-has-referenced-PrimaryKey. To show a join of two tables, you can follow these steps:
In the action of the controller write:
public ActionResult Details(int Id)
{
BussinceLogicLayer.UploadedFileBLL uploadBLL = new BussinceLogicLayer.UploadedFileBLL();
var details = uploadBLL.Details(Id).ToList();
return View(details);
}
Where method uploadBLL.Details(int id) is defined as below:
public List<table1HasForeinKey> Details(int Id)
{
using (Entities1 context = new Entities1())
{
var uploadList = context.table1HasForeinKey.Include("Table2-has-referenced-PrimaryKey").Where(x => x.PrimaryKeyInTable1 == Id).ToList();
return uploadList;
}
}
Upvotes: 0
Reputation: 11964
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)
Upvotes: 5