Error: The model item passed to the dictionary is of type X but this dictionary requires a model item of type Y

-1

It is giving error when starting the query and in the generation of the list with the results:

Error:

  

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Linq.Enumerable + WhereSelectEnumerableIterator' 2 [System.Linq.IGrouping'2 [System.Int32, BlogWeb.Models.Hashing], < & gt ; f__AnonymousType1'3 [System.Int32, System.Int32, System.Decimal]] ', but this dictionary requires a model item of type' System.Collections.Generic.IList'1 [BlogWeb.Models.Hashing] '.

Supply Controller:

public ActionResult AbastecimentoResumo()
{
    //código irrelevante

    var resultado = consulta.GroupBy(c => c.NumCarro.NCarro)
                            .Select(gp => new
                                         {
                                             NumCarroId = gp.Key,
                                             LitroTotal = gp.Sum(c => c.Litro),
                                             TotalConsumido = gp.Sum(c => c.TotalGasto)
                                          });            

    // código irrelevante

    return View(resultado);        
}

My view :

@using PagedList.Mvc;
@model IList<BlogWeb.Models.Abastecimento>

@{ ViewBag.Title = "Relatório de Vendas";
    Layout = "~/Views/Shared/_Layout.cshtml"; }

<table id="customers">
    <tr>
        <th>N° Carro </th>
        <th>Quant. </th>
        <th>Valor Unitário </th>
    </tr>
    @{ 
        foreach (var a in Model)
        {
            <tr>
                <td> @a.NumCarro.NCarro </td>
                <td> @a.Litro</td>
                <td> @a.TotalGasto</td>
            </tr>
        }
    }
</table>

ModelAccess:

public class Abastecimento
{
    public virtual int Id { get; set;}
    [Required]
    public virtual int Litro { get; set; }
    public virtual DateTime? DtAbastecido { get; set; }
    public virtual decimal VlrUnit { get; set; }
    public virtual int Km { get; set; }
    public virtual decimal TotalGasto { get; set; }
    public virtual int Km_Andado { get; set; }
    public virtual Usuario Autor { get; set; }
    public virtual Compra NomeProduto { get; set; }
    public virtual Veiculo NumCarro { get; set; }
}
    
asked by anonymous 02.02.2018 / 20:27

1 answer

2

See, the error says that the types are not the same .

All you need to do now is to get them to agree

Create a new class, I'll call it ViewModel because I can not identify the naming pattern you're using

public class ViewModel
{
    public int NumCarroId { get; set; }
    public int LitroTotal { get; set; }
    public decimal TotalConsumido { get; set; }
}

In view , change the second line to

@model IList<SeuNamespaceAqui.ViewModel>

And inside the table, you'll have to use the ViewModel fields of course

foreach (var a in Model)
{
    <tr>
        <td> @a.NumCarroId  </td>
        <td> @a.LitroTotal </td>
        <td> @a.TotalConsumido </td>
    </tr>
}

And in the GroupBy method, change Select(gp => new { }) to Select(gp => new ViewModel { })

    
02.02.2018 / 20:44