我试图将ASP.NET MVC项目从直观视图和模型更改为使用ViewModel中间层。基本上,我在做什么包括有人与零或更多事件与他们相关联。我将更改为ViewModel,因为我想在人详细信息页面上显示部分视图,该页面显示所有关联的事件。如何配置ViewModel和部分页面以包含IEnumerable
我有事件一个人模型类和一个,这是我的ViewModel:
public class PersonEventViewModel
{
public PersonEventViewModel(Person person)
{
Person = person;
}
public Person Person { get; set; }
public IEnumerable<Event> Events { get; set; }
}
这种转换我的单记录页的罚款。在诸如人物细节等页面上,我必须更改@Html.DisplayFor(model => model.CreatedOn)
这样的线条到@Html.DisplayFor(model => model.Person.CreatedOn)
,以便它可以正确找到属性,但它工作正常。然而,在我试图制作的部分视图中,似乎无法让它们在那里显示模型的属性。它看起来或多或少是这样的:
@model IEnumerable<Proj.ViewModels.PersonEventViewModel>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.RecordedOn)</th>
<th>@Html.DisplayNameFor(model => model.RecordedBy)</th>
<th>@Html.DisplayNameFor(model => model.Comments)</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.RecordedOn)</td>
<td>@Html.DisplayFor(modelItem => item.RecordedBy)</td>
<td>@Html.DisplayFor(modelItem => item.Comments)</td>
</tr>
}
这个工作时,它是直接使用活动的模式,但现在它似乎没有什么事,我把在model
和物业之间,它总是告诉我,它无法解决它。
此外,我不知道如何将ViewModel传递到局部视图。我还没有想出什么要放在这里:
@Html.RenderPartial("_PersonEventList", ?????)
我通过模型或模型信息的各种尝试都遇到了错误。
根据要求,这是我的详细信息页面的来源,我试图放置事件的部分视图。
@model Proj.ViewModels.PersonEventViewModel
@{ ViewBag.Title = "Details"; }
<h2>Details</h2>
<div>
<h4>Person</h4> <hr/>
<dl class="dl-horizontal">
<dt>@Html.DisplayNameFor(model => model.Person.FirstName)</dt>
<dd>@Html.DisplayFor(model => model.Person.FirstName)</dd>
@*...and so on through the fields...*@
</dl>
</div>
<p> @Html.ActionLink("Edit Record", "Edit", new {id = Model.Person.PersonId}) </p>
@Html.RenderPartial("_PersonEventList", Model)
你尝试过这样的@ Html.RenderPartial( “_ PersonEventList”(名单)ViewBag.data) –
我没有,但仍然给我一个错误,类似于之前所说的:'不能将类型'void'隐式转换为'object'。表达式必须返回一个值来呈现.' – techturtle
尝试类似这样的操作,@ Html.Partial(“View”,Model); @ {HTML。RenderPartial(“viewName”,Model));} –