C# ICollection和IEnumerable之间的索引视图冲突
我设置了一个创建和编辑视图,使用下拉框从关系表中输入数据。我混合了Critvit表中的1对多和多对1链接,无法让它们在索引视图中协同工作 克里特维特模型C# ICollection和IEnumerable之间的索引视图冲突,c#,html,razor,asp.net-mvc-5,C#,Html,Razor,Asp.net Mvc 5,我设置了一个创建和编辑视图,使用下拉框从关系表中输入数据。我混合了Critvit表中的1对多和多对1链接,无法让它们在索引视图中协同工作 克里特维特模型 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace NextRung.Models { public cla
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
名称空间NextRung.Models
{
公共类Critvit
{
公共Int32标准{get;set;}
公共Int32角色ID{get;set;}
公共十进制?工资{get;set;}
公共十进制角色exp{get;set;}
public Int32 SkillId{get;set;}
[数据类型(DataType.Date)]
公共日期时间?开始日期{get;set;}
[数据类型(DataType.Date)]
公共日期时间?结束日期{get;set;}
[数据类型(数据类型.日期时间)]
公共日期时间?CreateDate{get;set;}
公共字符串摘要{get;set;}
公共Int32公司ID{get;set;}
//海选资讯
公共Int32用户标识{get;set;}
[数据类型(数据类型.日期时间)]
公共日期时间?CreatedDate{get;set;}
[数据类型(数据类型.日期时间)]
公共日期时间?编辑日期{get;set;}
[数据类型(数据类型.日期时间)]
公共日期时间?删除日期{get;set;}
公共虚拟ICollection角色{get;set;}
公共虚拟ICollection技能{get;set;}
公共虚拟用户{get;set;}
公共虚拟公司{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>
@model i采集
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.companys.CompanyName)
@DisplayNameFor(model=>model.Users.Email)
@DisplayNameFor(model=>model.Roles.RoleTitle)
@DisplayNameFor(model=>model.Salary)
@DisplayNameFor(model=>model.RoleExp)
@DisplayNameFor(model=>model.Skills.SkillName)
@DisplayNameFor(model=>model.StartDate)
@DisplayNameFor(model=>model.EndDate)
@DisplayNameFor(model=>model.Summary)
@foreach(模型中的var项目){
@DisplayFor(modeleItem=>item.companys.CompanyName)
@DisplayFor(modeleItem=>item.Users.Email)
@DisplayFor(modelItem=>item.Roles.RoleTitle)
@DisplayFor(modelItem=>item.Salary)
@DisplayFor(modeleItem=>item.RoleExp)
@DisplayFor(modelItem=>item.Skills.SkillName)
@DisplayFor(modelItem=>item.StartDate)
@DisplayFor(modelItem=>item.EndDate)
@DisplayFor(modelItem=>item.Summary)
@ActionLink(“编辑”,“编辑”,新的{id=item.critid})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.critid})|
@ActionLink(“删除”,“删除”,新的{id=item.critid})
}
此代码似乎允许RoleTitle和SkillName字段工作,但不允许Email或CompanyName
我可以使用@model IEnumerable
如果我使用RoleId和SkillId,IEnumerable可以很好地工作,但这不是我需要的。到目前为止,我尝试的每个解决方案都只适用于某些领域。
那么,如何让索引视图显示所有4个字段中的关系数据,而不是仅显示2个字段 在模型中使用角色和技能列表
public virtual List<Role> Roles { get; set; }
public virtual List<Skill> Skills { get; set; }
公共虚拟列表角色{get;set;}
公共虚拟列表技能{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>
@foreach(model.Roles中的var r)
{
@Html.DisplayNameFor(r.RoleTitle)
}
@foreach(模型中的var s.Skills)
{
@Html.DisplayNameFor(r.RoleTitle)
}
角色和技能有多个对象,如何使用model.SkillName.SkillName
?我想每个技能应该有不同的技能名称,对吗?使用列表而不是Icollection。不,但我更喜欢列表,枚举在视图中有时不起作用。如何工作?在Ienumerable中键入的列表的函数与在列表中键入的函数完全相同,而不考虑您可以访问的某些方法。