【发布时间】:2015-04-29 03:15:59
【问题描述】:
我有一个创建和编辑视图设置为使用下拉框从关系表中输入数据。我在 Critvit 表中混合了 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 字段起作用,但不让 Email 或 CompanyName 起作用。
我可以用@model IEnumerable<NextRung.Models.Critvit>切换@model ICollection<NextRung.Models.Critvit>
如果我使用 RoleId 和 SkillId,IEnumerable 可以正常工作,但这不是我需要的。到目前为止,我尝试过的每个解决方案都只适用于某些领域。 那么,如何让索引视图显示所有 4 个字段而不是 2 个字段中的关系数据?
【问题讨论】:
-
角色和技能有多个对象,
model.Skills.SkillName怎么用?我想每个技能应该有不同的技能名称,对吧? -
使用 List 而不是 Icollection。
标签: c# html razor asp.net-mvc-5