2017-10-20 58 views
-1

Bonjour J'ai un Model de donnée qui s'appelle Utilisateur. Dans Controller je récupère tous les utilisateurs avec method linq et je veux les afficher dans la vu. c'est assez simple, mais je rencontre un problème que je n'arrive pas à résoudre .l'erreur est System.InvalidOperationException: L'élément de modèle passé dans le dictionnaire est de type « 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] ». j'ai essayé avec IEnumerable lstUtil= ctx.Utilisateur; Ca n'a rien changé. j'ai essayé ctx.Utilisateur.Toliste(); Ca n'a rien changé non plus...mvc linq controller view error

voici mes codes :

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

please in english –

+0

I have a dataModel which is called Utilisateur. i try in controller to collect all the datas in the table in the database and show them in my view , but i have a error about the type of the data sent by controller and what is expected in view. – Mania

+0

Stack Overflow requires content to be in English: https://meta.stackoverflow.com/questions/297673/how-do-i-deal-with-non-english-content –

Répondre

0

Ideally the below should work because in the Razor you are excepting Ienumerable.

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

Also, please double check the type of the Model. There is a conflict. One is "WebApplication1.Utilisateur‌​" and other is "WebApplication1.Models.Utilisateur‌​". This should fix your issue.

Update Razor to:

@model IEnumerable<WebApplication1.Utilisateur> 
+0

thank you for your answer but still i have a new error almost the same: L'élément de modèle passé dans le dictionnaire est de type « System.Collections.Generic.List'1[WebApplication1.Utilisateur] », mais ce dictionnaire requiert un élément de modèle de type « System.Collections.Generic.IEnumerable'1[WebApplication1.Models.Utilisateur] ». – Mania

+0

Please double check the type of the Model. There is a conflict. One is "WebApplication1.Utilisateur‌​" and other is "WebApplication1.Models.Utilisateur‌​". This should fix your issue. Update Razor code to: Update Razor to: @model IEnumerable Habeeb

+0

You are right! it was this! Thankyou very much – Mania