我有一个创建和编辑视图设置为使用下拉框从关系表输入数据。我有一个来自Critvit Table的1到多个和多个到1个链接的混合,并且不能让他们在索引视图上一起工作。
Critvit模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace NextRung.Models
{
public class Critvit
{
public Int32 CritvitId { get; set; }
public Int32 RoleId { get; set; }
public decimal? Salary { get; set; }
public decimal RoleExp { get; set; }
public Int32 SkillId { get; set; }
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime? CreateDate { get; set; }
public string Summary { get; set; }
public Int32 CompanyId { get; set; }
// AuditInfo
public Int32 UserId { get; set; }
[DataType(DataType.DateTime)]
public DateTime? CreatedDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime? EditedDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime? DeletedDate { get; set; }
public virtual ICollection<Role> Roles { get; set; }
public virtual ICollection<Skill> Skills { get; set; }
public virtual User Users { get; set; }
public virtual Company Companies { get; set; }
}
}
索引视图
@model ICollection<NextRung.Models.Critvit>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Companies.CompanyName)
</th>
<th>
@Html.DisplayNameFor(model => model.Users.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Roles.RoleTitle)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th>
@Html.DisplayNameFor(model => model.RoleExp)
</th>
<th>
@Html.DisplayNameFor(model => model.Skills.SkillName)
</th>
<th>
@Html.DisplayNameFor(model => model.StartDate)
</th>
<th>
@Html.DisplayNameFor(model => model.EndDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Summary)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Companies.CompanyName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Users.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Roles.RoleTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.RoleExp)
</td>
<td>
@Html.DisplayFor(modelItem => item.Skills.SkillName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Summary)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CritvitId }) |
@Html.ActionLink("Details", "Details", new { id=item.CritvitId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CritvitId })
</td>
</tr>
}
</table>
此代码似乎让RoleTitle和SkillName字段有效,但不是电子邮件或公司名称。
我可以使用@model ICollection<NextRung.Models.Critvit>
@model IEnumerable<NextRung.Models.Critvit>
如果我使用RoleId和SkillId,IEnumerable工作正常,但这不是我需要的。到目前为止,我尝试的每个解决方案仅适用于某些领域。 那么,如何让索引视图在所有4个字段中显示关系数据而不仅仅是2?
答案 0 :(得分:1)
在模型中使用列表中的角色和技能。
public virtual List<Role> Roles { get; set; }
public virtual List<Skill> Skills { get; set; }
在视图中将列表迭代到这些列表中的每个值,如下所示
<th>
@foreach(var r in model.Roles)
{
<p>
@Html.DisplayNameFor(r.RoleTitle)
</p>
}
</th>
<th>
@foreach(var s in model.Skills)
{
<p>
@Html.DisplayNameFor(r.RoleTitle)
</p>
}
</th>