2017-10-20 110 views
-1

你好,我有一個叫做用戶的數據模型。在控制器我與LINQ方法的所有用戶,我想在視圖中顯示。這是很簡單的,但我有一個問題,我解決不了.l'erreur是System.InvalidOperationException:傳遞到字典的模型項的類型是「System.Data.Linq.Table 1[WebApplication1.Utilisateur] », mais ce dictionnaire requiert un élément de modèle de type « System.Collections.Generic.IEnumerable 1 [WebApplication1.Models.Utilisateur]「。 我試圖與IEnumerable的lstUtil = ctx.Utilisateur;它改變了什麼。我試圖ctx.Utilisateur.Toliste();它也不改變......MVC LINQ視圖控制器錯誤

這裏是我的代碼:

public class Utilisateur 
 
    { 
 
     [Required] 
 
     [DisplayName("Login")] 
 
     public string Name { get; set; } 
 
     [Required] 
 
     [DisplayName("Mot de passe")] 
 
     [DataType(DataType.Password)] 
 
     public string Pass { get; set; } 
 
    } 
 
    
 
    public class TestController : Controller 
 
    { 
 
       public ActionResult Index() 
 
     { 
 
         using (var ctx = new BddDataClassesDataContext()) 
 
      { 
 
       var lstUtil = ctx.Utilisateur; 
 
           return View(lstUtil); 
 
      }     
 
     } 
 
    } 
 
    
 
    index.cshtml: 
 
    
 
    @model IEnumerable<WebApplication1.Models.Utilisateur> 
 

 
@{ 
 
    ViewBag.Title = "Index"; 
 
} 
 

 
<h2>Index</h2> 
 

 
<p> 
 
    @Html.ActionLink("Create New", "Create") 
 
</p> 
 
<table class="table"> 
 
    <tr> 
 
     <th> 
 
      @Html.DisplayNameFor(model => model.Name) 
 
     </th> 
 
     <th> 
 
      @Html.DisplayNameFor(model => model.Pass) 
 
     </th> 
 
     
 
     <th></th> 
 
    </tr> 
 

 
@foreach (var item in Model) { 
 
    <tr> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.Name) 
 
     </td> 
 
     <td> 
 
      @Html.DisplayFor(modelItem => item.Pass) 
 
     </td> 
 
    
 
     <td> 
 
      @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
 
     </td> 
 
    </tr> 
 
} 
 

 

 
    
 

 
</table>

+2

請用英文我有一個 –

+0

數據模型歸仁s'intitule用戶。我嘗試檢查,收集所有的DATAS在數據庫中的表和展示給我看在我看來,針對我有一個關於該種支票和期望是什麼鑑於發送的數據錯誤。 – Mania

+0

堆棧溢出需要很高興能在英語:https://meta.stackoverflow.com/questions/297673/how-do-i-deal-with-non-english-content –

回答

0

理想下,應在剃刀工作,因爲你是IEnumerable的除外。

var lstUtil = ctx.Utilisateur; 
return View(lstUtil.ToList()); 

另外,請仔細檢查類型的模型。有衝突。一個是「WebApplication1.Utilisateur」等是「WebApplication1.Models.Utilisateur」。這不宜解決您的問題。

更新剃刀:

@model IEnumerable<WebApplication1.Utilisateur> 
+0

謝謝你的回答,但我仍然有一個幾乎是新的錯誤LA即使得到:傳遞到字典的模型項的類型是「System.Collections.Generic.List'1 [WebApplication1.Utilisateur]」,但這部詞典需要一種「模板System.Collections中的項目。 Generic.IEnumerable'1 [WebApplication1.Models.Utilisateur]「。 – Mania

+0

請仔細檢查類型的模型。有衝突。一個是「WebApplication1.Utilisateur」等是「WebApplication1.Models.Utilisateur」。這不宜解決您的問題。更新到 剃刀代碼:更新剃刀:@model的IEnumerable Habeeb

+0

你是對的!這是這個!三江源非常 – Mania