I am trying to display the name of an author of a question but unlike the other properties of the model, the ApplicationUser.UserName property does not show in the view (it's just blank). I am using Entity Framework, MVC 5 and Razor.
My Question model:
public class Question { public int QuestionID { get; set; } public string Title { get; set; } public ApplicationUser Author { get; set; } public string Description { get; set; } [DisplayName("Created")] public DateTime CreationDateTime { get; set; } public int Rating { get; set; } } My QuestionController Index action:
// GET: Questions public ActionResult Index() { return View(db.Questions.ToList()); } And the view:
@model IEnumerable<Waegogi.Models.Question> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Author.UserName) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th> @Html.DisplayNameFor(model => model.CreationDateTime) </th> <th> @Html.DisplayNameFor(model => model.Rating) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Author.UserName) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.DisplayFor(modelItem => item.CreationDateTime) </td> <td> @Html.DisplayFor(modelItem => item.Rating) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.QuestionID }) | @Html.ActionLink("Details", "Details", new { id=item.QuestionID }) | @Html.ActionLink("Delete", "Delete", new { id=item.QuestionID }) </td> </tr> } </table> Where am I going wrong?