使用 .net mvc 5 列出多对多表关系中的数据

我是 Asp.Net MVC 的学生开发者。我正在做一些小的 .Net MVC 项目来学习它自己的项目,我第一次面对表之间的多对多关系。我有两个具有多对多关系的模型usersModel和groupsModel。它们usersgroupModel以多对多的方式关联。在我的家庭控制器中,我试图用 LinkQ 代码列出表格的内容。但我没有。当我运行我的项目时,我的Index.cshtml模型正在变为空值。在这种情况下,我的观点向我发送了一个错误,例如


'System.Data.Entity.Infrastructure.DbQuery 1[<>f__AnonymousType22[System.String,System.String]]',但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable`1[Agm.Models.EntityFramework.Groups ]'...


我可以做什么根据我的索引页面中的 userId 和 groupId 列出我的数据?你能帮我解决这个问题吗?从现在开始谢谢。


我的用户模型;


    public class usersModel

        {

    public int userId { get; set; }

    public string userNameSurname { get; set; }

public virtual ICollection<Groups> Groups { get; set; }

    }

我的团体模型:


 public class groupsModel

    {

 public int groupId { get; set; }

        public string groupName { get; set; }

public virtual ICollection<Users> Users { get; set; }

}

我的控制器:


public ActionResult Index()

        {   

             var userGroups = from g in db.Groups

                             from u in g.Users

                             select new

                             {

                               g.groupName,

                               g.groupImageUrl

                             };

           return View(userGroups);

        }

我的索引页:


@model IEnumerable<Agm.Models.EntityFramework.Groups>


<table class="table">

    <tr>

        <th>

            @Html.DisplayNameFor(model => model.groupName)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.groupImageUrl)

        </th>

        <th></th>

    </tr>


@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.groupName)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.groupImageUrl)

        </td>

    </tr>

}


</table>


胡说叔叔
浏览 52回答 1
1回答

jeck猫

像这样创建视图模型:&nbsp;public class UsersGroupViewModel&nbsp;{&nbsp; &nbsp; &nbsp;public string GroupName{ get; set; }&nbsp; &nbsp; &nbsp;public string GroupImageUrl{ get; set; }&nbsp;}将您的操作更改为此:public ActionResult Index()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var userGroups = (from g in db.Groups&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from u in g.Users&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new UsersGroupViewModel()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GroupName = g.groupName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GroupImageUrl = g.groupImageUrl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return View(userGroups);&nbsp; &nbsp; &nbsp; &nbsp; }并将视图模型更改为此:@model List<UsersGroupViewModel>
打开App,查看更多内容
随时随地看视频慕课网APP