使用局部视图时的空模型

时间:2015-11-20 14:29:20

标签: c# asp.net-mvc asp.net-mvc-4 ef-code-first

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

这是我的小说模特:

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>

我的部分视图(_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>

1 个答案:

答案 0 :(得分:4)

如果您查看主视图,您会看到您传递空列表:

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

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

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