VB MVC连接表返回视图

时间:2015-04-30 02:34:57

标签: vb.net razor model-view-controller

我真的不知道。

我正在尝试返回一个包含连接表结果的视图。

我一直收到这个错误:

“传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery`1 [System.String []]',但此字典需要类型为”tacticMVC“的模型项。加密'。“

我的控制器代码如下

 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

如果我只使用函数中的2个注释行,则视图返回正常但当然我只能从一个表中获取数据。我需要加入表格,然后返回数据。

我尝试添加.toList(),。sortordefault()等 - 没有解决任何问题

vbhtml文件:

@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

2 个答案:

答案 0 :(得分:1)

您正在直接返回数据库查询对象,但您的网页的cshtml表示它需要一个类型化的ViewModel。

tacticMVC.crypto在哪里定义,它看起来像什么?

Dim crypto并不意味着“crypto类型的对象”,它意味着“一个名为crypto的后期绑定对象”。

答案 1 :(得分:0)

大! - 就像花了好几个小时后的许多其他经历一样,我在发布问题后立即解决了这个问题。

答案:

控制器

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>

我愿意接受更好的解决方案

由于