Reputation: 6326
I have a profile page and inside that I have a partial view that lists the users items they've created on the site. I've set the whole site up using code-first development and have a test Context initialiser that populates the database with a couple of user and a couple of items. The users part works fine but the items will not display on the partial view - when debugging, I found that the 'Novels' (items) model was empty, despite having data to be populated with.
Here's my Novels Model:
public class Novel
{
public Int32 NovelId { get; set; }
[Display(Name = @"Working Title")]
[Required(ErrorMessageResourceType = typeof (Resource.Novel), ErrorMessageResourceName = "TitleRequired")]
public string Title { get; set; }
[Display(Name = @"Category")]
[Required(ErrorMessageResourceType = typeof (Resource.Novel), ErrorMessageResourceName = "GenreRequired")]
public string Genre { get; set; }
[Display(Name = @"Outline")]
[DataType(DataType.MultilineText)]
[Required(ErrorMessageResourceType = typeof (Resource.Novel), ErrorMessageResourceName = "AbstractRequired")]
public string Abstract { get; set; }
public bool Privacy { get; set; }
public Int32 AccountId { get; set; }
public virtual Account Account { get; set; }
}
My Account Model:
public class Account
{
public Int32 AccountId { get; set;}
[Required(ErrorMessageResourceType = typeof (Resource.Account), ErrorMessageResourceName = "UsernameRequired")]
[DisplayName(@"User Name")]
public string Username { get; set; }
[Required(ErrorMessageResourceType = typeof (Resource.Account), ErrorMessageResourceName = "ForenameRequired")]
[DisplayName(@"Forename")]
public string FirstName { get; set; }
[Required(ErrorMessageResourceType = typeof (Resource.Account), ErrorMessageResourceName = "SurnameRequired")]
[DisplayName(@"Surname")]
public string Surname { get; set; }
[Required(ErrorMessageResourceType = typeof (Resource.Account), ErrorMessageResourceName = "EmailRequired")]
[DataType(DataType.EmailAddress)]
[DisplayName(@"Email Address")]
public string Email { get; set; }
[DisplayName(@"User Picture")]
public string Picture { get; set; }
[DisplayName(@"Full Name")]
public string FullName
{
get
{
var name = FirstName + " " + Surname;
return name;
}
}
public virtual List<Novel> Novels { get; set; }
}
My Profile Page:
@using TheNovelMachine.Models
@model TheNovelMachine.Models.Account
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<img id="homeimage" src="~/Image/front-page-book.jpg" alt="The Novel Machine" />
<div id="text" class="col-lg-12">
<h1>Profile</h1>
<div class="col-lg-6 no-gutter">
<h2>@Html.DisplayFor(model => model.Username)</h2>
<h4>@Html.DisplayFor(model => model.FullName)</h4>
<p>@Html.DisplayFor(model => model.Email)</p>
</div>
<div class="col-lg-6 pull-right text-right no-gutter">
<img class="profilepic" src="~/Image/@(Html.DisplayFor(model => model.Username)).jpg" />
</div>
<div class="col-lg-12 no-gutter">
<hr/>
<h4>@Html.DisplayFor(model => model.Username)'s Novels</h4>
@{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", new List<Novel>());
}
</div>
</div>
And my partial view (_NovelProfile.cshtml):
@using System.Web.DynamicData
@using TheNovelMachine.Models
@model IEnumerable<TheNovelMachine.Models.Novel>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Abstract)
</th>
<th>
@Htm
l.DisplayNameFor(model => model.Privacy)
</th>
<th>
@Html.DisplayNameFor(model => model.AccountId)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Abstract)
</td>
<td>
@Html.DisplayFor(modelItem => item.Privacy)
</td>
<td>
@Html.DisplayFor(modelItem => item.AccountId)
</td>
</tr>
}
</table>
Upvotes: 2
Views: 3546
Reputation: 12491
If you take a look at your main View you see that you pass empty list:
@{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", new List<Novel>());
}
I suppose you want to pass Model property, don't you?
@{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", Model.Novels);
}
Upvotes: 4