MVC传递两个对象来查看

时间:2015-12-08 09:50:56

标签: vb.net asp.net-mvc-4 model-view-controller razor

我正在努力将两个对象传递给我的视图,然后显示数据。为了将两个不同的对象发送到一个视图,我为它分隔了一个类,它将包含我的两个类对象,如下所示:

Public Class CustomModelProjetsTransports
    Public Projects As IEnumerable(Of woitgroup_transport.tbProjekt)
    Public Transports As IEnumerable(Of woitgroup_transport.tbTransport)
End Class

现在在我的控制下,我正在看它(我检查过,数据在那里:

Function Index() As ActionResult
        If Not IsNothing(Session("LogedUserId")) Then

            Dim userId As Integer = Session("LogedUserId")
            Dim projectsAndTransportsLists As New CustomModelProjetsTransports

            Dim ProjectsPerUser As New List(Of tbProjekt)
            ProjectsPerUser = db.Database.SqlQuery(Of tbProjekt)("SELECT * FROM [mydb].[dbo].[tbProjekt] where Id IN (SELECT DISTINCT ProjectId FROM [mydb].[dbo].[tbUserProject] WHERE UserId = " & Session("LogedUserId") & ")").ToList

            Dim transportsPerUser As New List(Of tbTransport)
            transportsPerUser = db.Database.SqlQuery(Of tbTransport)("SELECT * FROM [mydb].[dbo].[tbTransport] where ProjectId IN  (SELECT DISTINCT ProjectId FROM [mydb].[dbo].[tbUserProject] WHERE UserId = " & Session("LogedUserId") & ")").ToList

            projectsAndTransportsLists.Projects = ProjectsPerUser
            projectsAndTransportsLists.Transports = transportsPerUser

            Return View(projectsAndTransportsLists)

        Else
            Return RedirectToAction("Index", "Login")
        End If
    End Function

问题出现在View:

这是最重要的:

@ModelType IEnumerable(Of woitgroup_transport.CustomModelProjetsTransports)

  <tbody>
                                                @For Each item In Model(1).Transports.ToList
                                                    Dim currentItem = item
                                                    @<tr>
...

当我执行我的应用程序时,它在这里填写正确:

   Return View(projectsAndTransportsLists)

但是当视图想要显示如下时,我正在发现错误:

The model item passed into the dictionary is of type 'woitgroup_transport.CustomModelProjetsTransports', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[woitgroup_transport.CustomModelProjetsTransports]'.

其他问题:

请注意我在我的视图中有项目列表如何正确填写下拉列表(最好使用引导程序样式)。我这样想,但还是有些不对劲......

@Html.DropDownListFor(Function(m) m.Projects.., DirectCast(Model.Projects, SelectList), New With { _
                        .class = "form-control" _
                        })

它还说Function(m) m.Projects.Name

that Name is not exist - 我只看到关于知识分子的通用列表,如函数(m)m.Projects.ToList ...

1 个答案:

答案 0 :(得分:0)

错误消息非常清楚该问题。您正在将CustomModelProjetsTransports传递给视图。但该视图需要IEnumerable(Of CustomModelProjetsTransports)。删除IEnumerable,你应该没事。