C# 在ASP.NET MVC 4中列出来自对象的ICollection

C# 在ASP.NET MVC 4中列出来自对象的ICollection,c#,asp.net-mvc-4,razor-2,C#,Asp.net Mvc 4,Razor 2,我有一个像这样的用户类 Public class User { public int UserId { get; set; } public string UserName { get; set; } [Display(Name = "First Name")] public string FullName { get; set; } [Display(Name = "Last Name")]

我有一个像这样的用户类

Public class User
    {
        public int UserId { get; set; }

        public string UserName { get; set; }

        [Display(Name = "First Name")]
        public string FullName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }



        public virtual ICollection<Project> Projects { get; set; }
}
这是我的专题课

public class Project
    {
        [Key]
        public int ProjectId { get; set; }



        public int ProjectTypeId { get; set; }
        public int UserId { get; set; }

        [Display(Name = "Project title")]
        public string ProjectName { get; set; }
        public string ProjectDescription { get; set; }

        public virtual ProjectDetail ProjectDetail { get; set; }

        public virtual ICollection<ProjectUpload> ProjectUploads { get; set; }

        [ForeignKey("ProjectTypeId")]
        public virtual ProjectType ProjectType { get; set; } 

        [ForeignKey("UserId")]
        public virtual User User { get; set; }

    }

我想在用户索引视图中列出特定用户的项目。我该怎么做。

好吧,它将是这样的:

->Index.cshtml


我也有类似的问题,但我需要将ICollection发回控制器。这是一件痛苦的事情,因为每个输入的Name属性都必须是唯一的,而MVC只会将其作为模型的一部分进行相应的命名。因此,如果其他人需要它,这里是语法

<table>
    @if (@Model != null && @Model.Projects != null)
    {

        for (var i = 0; i < Model.Projects.Count; i++)
        {
            <tr>
                <td>
                    @Html.Hidden("Projects[" + i + "].ProjectId", Model.Projects.ElementAt(i).ProjectId)
                    @Html.Label("Projects[" + i + "].ProjectName", Model.Projects.ElementAt(i).ProjectName)
                </td>
                <td>
                    @Html.TextBox("Projects[" + i + "].ProjectName", Model.Projects.ElementAt(i).ProjectName, new { @class = "form-control" })
                </td>
            </tr>
        }
    }
</table>
<table>
    @if (@Model != null && @Model.Projects != null)
    {

        for (var i = 0; i < Model.Projects.Count; i++)
        {
            <tr>
                <td>
                    @Html.Hidden("Projects[" + i + "].ProjectId", Model.Projects.ElementAt(i).ProjectId)
                    @Html.Label("Projects[" + i + "].ProjectName", Model.Projects.ElementAt(i).ProjectName)
                </td>
                <td>
                    @Html.TextBox("Projects[" + i + "].ProjectName", Model.Projects.ElementAt(i).ProjectName, new { @class = "form-control" })
                </td>
            </tr>
        }
    }
</table>