【发布时间】:2014-06-19 20:48:16
【问题描述】:
我遇到了一个问题,我无法在视图中显示多个模型,特别是在表单中。到目前为止,我一直采用的方法是使用一个“父”视图来保存与各种模型相关的多个局部视图(使用 EF6、VS13)。
但是,由于错误的实现或理解,我似乎无法完成这项工作,因为我不断收到每个视图只能与一个模型相关的错误。
--模型--
新员工:
namespace HROnboarding.Domain.Entities
{
public class NewHire
{
public int NewHireId { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }
public string Department { get; set; }
public string Position { get; set; }
[DisplayName("Needs Phone")]
public bool NeedsPhone { get; set; }
[DisplayName("Needs Computer")]
public bool NeedsComputer { get; set; } //Enables SoftwareNeeded, External Devices(Keyboard, mouse, docking station, etc), Resource Permission(text box?), Email groups
[DisplayName("Needs Credit Card")]
public bool NeedsCredit { get; set; }
}
}
所需软件:
namespace HROnboarding.Domain.Entities
{
public class SoftwareNeeded
{
//Primitive Properties
public int SoftwareNeededId { get; set; }
public string SoftwareName { get; set; } //piece of software (GIMP, VS13, etc)
public bool IsNeeded { get; set; } //whether or not it is needed by the user
}
}
--控制器--
NewHireController:
namespace HROnboarding.WebUI.Controllers
{
public class NewHireController : Controller
{
private INewHiresRepository repository;
public NewHireController(INewHiresRepository newHireRepository)
{
//instantiate repository
this.repository = newHireRepository;
}
//
// GET: /NewHire/
public ViewResult Index()
{
return View(repository.NewHires);
}
public ViewResult NewHiresView()
{
return View(repository.NewHires);
}
}
}
注意:还有一个对应的 SoftwareController,与 NewHireController 相同。
这些模型对应于每个模型看起来像这样的接口(存储库):
namespace HROnboarding.Domain.Interfaces
{
public interface INewHiresRepository
{
IEnumerable<NewHire> NewHires { get; }
int SaveNewHire(NewHire newHire);
NewHire GetById(int Id);
}
}
正如我所说,我正在尝试为每个模型使用部分视图:
@model IEnumerable<HROnboarding.Domain.Entities.NewHire>
@if (Model != null)
{
<div>
<table>
<thead>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Department
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@item.FirstName
.
.
.
我为每个模型使用其中一个部分视图,通过主索引视图将它们包装到引用中,如下所示:
@using HROnboarding.Domain.Entities
@using HROnboarding.Domain.Interfaces
@model IEnumerable<HROnboarding.Domain.Entities.NewHire>
<h2>List</h2>
<p>
<div>
@Html.Partial("NewHiresView")
</div>
<div>
@*@Html.Partial("Softwares")*@ @*<-------Can't just add this in?*@
</div>
</p>
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
@*<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
但是当我去添加各种部分到索引视图时,我只能添加 NewHire。当我添加软件视图时,它会抛出一个未处理的异常:
附加信息:传入字典的模型项是 类型 'System.Data.Entity.DbSet
1[HROnboarding.Domain.Entities.NewHire]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[HROnboarding.Domain.Entities.SoftwareNeeded]'
正如我所说,我必须在这里遗漏一些基本的东西,因为我觉得我把它弄得太复杂了。也不确定这是否是“太多”的背景信息,但我希望我的问题很清楚。
【问题讨论】:
-
根据定义,你只能有一个模型,没有一个视图有多个模型。如果您需要渲染来自多个不同类的数据,您可以创建一个特定于您的视图的视图模型,并将您想要渲染的那些类包含为您的视图模型的成员。而且您可能希望使用编辑器模板而不是部分视图。
标签: c# asp.net-mvc entity-framework model-view-controller