I'm trying to get my head around ASP.Net MVC having come from a Web Forms background.
I've got a MySQL database with 2 entities:
artwork - id(PK), artistid(FK), name...
artist - id(PK), artist, website...
I've created an ADO.Net Entity Data Model using this database and ArtistController.cs and ArtworkController.cs, which has created the corresponding views.
Now when I access http://localhost:49664/Artwork, the page displays a list of all the rows in the artwork table, however, it displays the artistid. How would I go about displaying the artist's name instead?
UPDATE:
In the database, the field in the artist table is named 'artist' but when creating the Entity Data Model, Visual Studio has named it as 'artist1'.
I've tried '@Html.DisplayFor(modelItem => item.artist.artist)' but that still displays the artist id.
Artwork class:
public partial class artwork
{
public int id { get; set; }
public Nullable<int> artistId { get; set; }
public string title { get; set; }
public Nullable<int> editionId { get; set; }
public virtual artist artist { get; set; }
public virtual edition edition { get; set; }
}
Artist class:
public partial class artist
{
public artist()
{
this.artworks = new HashSet<artwork>();
}
public int id { get; set; }
public string artist1 { get; set; }
public string website { get; set; }
public virtual ICollection<artwork> artworks { get; set; }
}
Artwork Controller:
public ActionResult Index()
{
var artworks = db.artworks.Include(a => a.artist).Include(a => a.edition);
return View(artworks.ToList());
}
Artwork view code:
@model IEnumerable<myflatfile.artwork>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.artist1)
</th>
<th>
@Html.DisplayNameFor(model => model.title)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.artist.artist1)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
</tr>
}
</table>