g r
g r

Reputation: 65

VB MVC Join Tables Return View

I really have no idea.

I am trying to return a view with results from a Joined Table.

I keep receiving this error:

"The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.String[]]', but this dictionary requires a model item of type 'tacticMVC.crypto'. "

my controller code is as follows

 Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
    If Request.IsAuthenticated = True Then

        'Dim cryptoes = db.cryptoes.Where(Function(c) c.key_email = HttpContext.User.Identity.Name()).ToList()
        'Dim crypto As crypto = db.cryptoes.Find(cryptoes(0).id_cypto)


        Dim crypto = (From c In db.cryptoes
                       Join con In db.consumers On c.id_consumer Equals con.id_consumer
                        Where c.id_consumer = 3
                      Select {c.key_email})

        Return View(crypto)
    Else
        Return HttpNotFound()
    End If
End Function

If I use just the 2 commented lines in the function the view returns fine but of course I can only get the data from one table. I need to join the tables and then return the data.

I've tried adding .toList(), .singleordefault() etc - does not solve anything

the vbhtml file:

@ModelType tacticMVC.crypto

<h2>Details</h2>
@Using Html.BeginForm("Action", "Controller", FormMethod.Post)
@<fieldset>

<div class="display-label">
    @Html.DisplayNameFor(Function(model) model.key_email)
</div>
<div class="display-field">
    @Html.DisplayFor(Function(model) model.key_email)
    something
</div>


</fieldset>
@<p>
    @Html.ActionLink("Edit", "Edit", New With {.id = Model.id_cypto}) |
    @Html.ActionLink("Back to List", "Index")
</p>
End Using

Upvotes: 0

Views: 227

Answers (2)

g r
g r

Reputation: 65

Great! - Just like a lot of other experiences after spending hours on it I have worked it out immediately after I posted my question.

Answer:

Controller

Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
    If Request.IsAuthenticated = True Then

        Dim cryptoes = db.cryptoes.Where(Function(c) c.key_email = HttpContext.User.Identity.Name()).ToList()
        Dim crypto As crypto = db.cryptoes.Find(cryptoes(0).id_cypto)

        Return View(crypto)
    Else
        Return HttpNotFound()
    End If
End Function

vbhtml

<div class="display-field">
     @Html.DisplayFor(Function(model) model.consumer.name_consumer)
 </div>

I am open to a better solution

Thanks

Upvotes: 0

Dai
Dai

Reputation: 155523

You're returning a database query object directly, but your page's cshtml says it expects a typed ViewModel.

Where is tacticMVC.crypto defined and what does it look like?

Dim crypto does not mean "an object of type crypto", it means "A late-bound object named crypto".

Upvotes: 1

Related Questions