Rein
Rein

Reputation: 911

Passing joined table to razor view

I'm new to the MVC architecture and I have the following Controller that's joining two tables:

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

How should I set the @ModelType in the view in order to reference the values in q? My project name is ECRM and I can't seem to get VS2013 to recognize anything when I type in ECRM.Models

Joining tables seems to be giving me a lot more trouble than I initially would've thought!

Upvotes: 0

Views: 81

Answers (1)

Madison
Madison

Reputation: 411

Create a class for your return type if you don't already have one (simple example here)

Public Class YourClass
    Public ExampleValue As String
End Class

Then change the code in your controller to this

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

And set the Model type in the view like so

@ModelType List(Of MvcApplication1.ExampleClass)

Upvotes: 1

Related Questions