【问题标题】:Index View conflict between ICollection and IEnumerableICollection 和 IEnumerable 之间的索引视图冲突
【发布时间】: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&lt;NextRung.Models.Critvit&gt;切换@model ICollection&lt;NextRung.Models.Critvit&gt;

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

【问题讨论】:

  • 角色和技能有多个对象,model.Skills.SkillName怎么用?我想每个技能应该有不同的技能名称,对吧?
  • 使用 List 而不是 Icollection。

标签: c# html razor asp.net-mvc-5


【解决方案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>

【讨论】:

  • 不,但我更喜欢列表,可枚举有时在视图中不起作用
  • 不工作怎么办?输入到 Ienumerable 中的列表的功能与输入到列表中的方式完全相同,而忽略您有权访问的某些方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2012-03-06
相关资源
最近更新 更多