-3

I get this Exception but i don't how to fix it:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[DataModel.Gabarit]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[ViewModel.GabaritViewModel]'.

My Controller:

 public ActionResult Traitement(string designation)
    {
       GabaritRepository gabaritrepository = new GabaritRepository(db);
        var gabarits = gabaritrepository.Get(g => g.Designation == designation).ToList();

        return View(gabarits);
    }

My View:

@model IEnumerable<ViewModel.GabaritViewModel>   
@{
    ViewBag.Title = "Traitement";
}

<h2>Traitement</h2>    
<div class="col-xs-12">
    <div class="box">
        <h2>Gabarits</h2>

        <table class="table table-striped">
            <tr>
                <th>
                    Code à barre
                </th>
                <th>
                   Etat
                </th>
                <th>                      
                </th>                  
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.CodeBarre)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Etat)
                    </td>                                                                 
                    <td>    
                        @Html.ActionLink("Sortie", "Sortie", new {id = item.CodeBarre})                         
                    </td>
                </tr>
            }

        </table>
    </div>
</div>

GabaritViewModel:

   namespace ViewModel
{
    public class GabaritViewModel
    {
        public int CodeBarre { get; set; }
        public string Designation { get; set; }
        public string Photo { get; set; }
        public Nullable<int> Produit { get; set; }
        public Nullable<int> Poste { get; set; }
        public string Exemplaire { get; set; }
        public string Etat { get; set; }
        public int Id_Etat { get; set; }

       }

I have to pass ViewModel not DataModel and I don't know why I'm not allowed to.

oumaima
  • 71
  • 9
  • show code(properties) for your GabaritViewModel class and inside ur controller "return View(gabarits);" - should return List – Chandan Kumar Apr 10 '17 at 08:07
  • What part of the error don't you understand, what did your research show? You'll have to convert your `Gabarit` to `GabaritViewModel` instances somehow before passing it to `return View(model)`. – CodeCaster Apr 10 '17 at 08:09
  • hope your "gabarits" which is a list contains items and each item contains all the properties of 'GabaritViewModel' class. m i right/ if wrong, please show the values of a single item of "gabarits" – Chandan Kumar Apr 10 '17 at 08:11
  • @CodeCaster yes this is what i'm searching for..Where i have to do this change? – oumaima Apr 10 '17 at 08:12

1 Answers1

0

Your repositories .Get() method is returning a collection of type Garbarit, you need a collection of type GabaritViewModel. One option would be to do another select and manually map your properties:

public ActionResult Traitement(string designation)
{
    GabaritRepository gabaritrepository = new GabaritRepository(db);
    var gabarits = gabaritrepository.Get(g => g.Designation == designation)
                                    //Map your Gabarit to your ViewModel here
                                    .Select(x => new GabaritViewModel {
                                        CodeBarre = x.CodeBarre,
                                        Etat = x.Etat
                                    }).ToList();

    return View(gabarits);
}
Marco
  • 22,856
  • 9
  • 75
  • 124