gpt4 book ai didi

c# - 使用局部 View 时的空模型

转载 作者:行者123 更新时间:2023-11-30 20:39:59 26 4
gpt4 key购买 nike

我有一个个人资料页面,里面有一个部分 View ,其中列出了他们在网站上创建的用户项目。我已经使用代码优先开发设置了整个站点,并有一个测试上下文初始化程序,它用几个用户和几个项目填充数据库。用户部分工作正常,但项目不会显示在部分 View 中 - 在调试时,我发现“小说”(项目)模型是空的,尽管有数据要填充。

这是我的小说模型:

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; }
}

我的账户模型:

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; }

}

我的个人资料页面:

@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>

还有我的局部 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>

最佳答案

如果您查看主视图,您会发现传递的是空列表:

@{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", new List<Novel>());
}

我想你想传递 Model 属性,是吗?

@{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", Model.Novels);
}

关于c# - 使用局部 View 时的空模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com