2015-04-30 72 views
0

我真的不知道。VB MVC加入表格返回視圖

我想從結合表中返回結果。

我不斷收到此錯誤:

「傳遞到詞典中的模型項的類型爲‘System.Data.Entity.Infrastructure.DbQuery`1 [System.String []]’,但這個字典需要型「tacticMVC.crypto」的典範項目。「

我的控制器代碼如下

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個評論的功能線視圖返回不錯,但當然,我的只能得到t他從一張桌子上的數據。我需要加入表格然後返回數據。

我已經嘗試添加.toList(),.singleordefault()等 - 不能解決任何問題

的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 

回答

1

你直接返回一個數據庫查詢對象,但你的頁面的cshtml表示它期望一個類型化的ViewModel。

tacticMVC.crypto在哪裏定義,它看起來像什麼?

Dim crypto並不意味着「crypto」類型的對象,它表示「一個名爲crypto的晚期綁定對象」。

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> 

我打開一個更好的解決方案

感謝