索引视图冲突 ICollection 和 IEnumerable 之间的冲突



>我设置了一个创建和编辑视图,以使用下拉框从关系表中输入数据。我混合了来自 Critvit 表的 1 对多和多对 1 链接,并且无法让它们在索引视图上协同工作。

批判模型

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>

此代码似乎允许"角色标题"和"技能名称"字段正常工作,但不允许"电子邮件"或"公司名称"工作。

我可以用@model IEnumerable<NextRung.Models.Critvit>切换@model ICollection<NextRung.Models.Critvit>

如果我使用 RoleId 和 SkillId,IEnumerable 工作正常,但这不是我需要的。到目前为止,我尝试过的每个解决方案都仅适用于某些领域。那么,如何让索引视图显示所有 4 个字段中的关系数据,而不仅仅是 2 个字段?

在模型中,使用"列表"作为角色和技能。

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>

最新更新