将连接表传递给剃刀视图

时间:2015-01-11 02:10:09

标签: asp.net vb.net razor

我是MVC架构的新手,我有以下控制器加入两个表:

Public Class ProblemsController
    Inherits Controller
    Private db As New dbECRMEntities
    ' GET: Problems
    Function Index() As ActionResult
        Dim q = (From pb In db.Problem_History Join pbs In db.Problems On pb.problem_id Equals pbs.problem_id)
        Return View(q.AsEnumerable)
    End Function
End Class

如何在视图中设置@ModelType以引用q中的值?我的项目名称是ECRM,当我输入ECRM.Models时,我无法让VS2013识别任何内容

加入牌桌似乎给我带来了比我最初想的更多麻烦!

1 个答案:

答案 0 :(得分:1)

如果您还没有返回类型,请为其创建一个类(此处为简单示例)

Public Class YourClass
    Public ExampleValue As String
End Class

然后将控制器中的代码更改为

Public Class ProblemsController
    Inherits Controller
    Private db As New dbECRMEntities
    ' GET: Problems
    Function Index() As ActionResult
    Dim q = (From pb In db.Problem_History Join pbs In db.Problems On pb.problem_id Equals pbs.problem_id
         Select New YourClass With {.ExampleValue = pb.ExampleValue})
    Return View(q.AsEnumerable)
    End Function
End Class

并在视图中设置模型类型

@ModelType List(Of MvcApplication1.ExampleClass)