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

Asked

Viewed 1,906 times

-1

Error starting query and generating list with 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.Provisioning],<>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.Sourcing]'.

Abastecimento 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>

Modelling:

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; }
}
  • Young man, note my issue in your question, this is the relevant code so that it can be answered. That’s all we need to reproduce the problem.

  • Now, try to edit the question and put the class code BlogWeb.Models.Abastecimento, please.

  • @LINQ sorry, it’s just I thought it might be something up that could interfere, things up, I apologize

  • No need to apologize, I’m just trying to instruct you

  • muder . Select(gp => new .... to . Select(gp => new Supply

  • @Marconciliosouza Where is the property LitroTotal in kind Abastecimento? And the properties NumCarroId and TotalConsumido?

  • @LINQ ... he’s actually creating an Anonymous type with different names than it really should be ... in the typed class. a view waits Supply and he pass an Anonymous ...

  • That, I know. The question I asked you was another. How to change the type of Select the same as the view will solve the problem? It will only cause more problem.

  • @LINQ, we didn’t know that... sorry there .

Show 4 more comments

1 answer

1


See, the mistake says the guys aren’t the same.

All you need to do now is get them to agree

Create a new class, I’ll call it ViewModel because I can’t identify the nomenclature pattern you’re using

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

In the view, change the second line to

@model IList<SeuNamespaceAqui.ViewModel>

And inside the table, you will have to use the fields of ViewModel obviously

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

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

  • If I put it on for good LitroTotal, TotalConsumido put the same names, would have no problem and another doubt is that usually I use viewModel to do?

  • if you need me to post my Abastecimentomodel(Viewmodel) to help you

  • The guy has to be exactly the same. What you’re doing is just passing information from controller to view, no problem there.

  • But the types are same types, Two int’s and a decimal

  • @That doesn’t mean anything, they have to be exactly the same.

  • And something else to my parameter ConsultaResumo consult the supply, no problem?

  • LINQ has no way to continue this on Monday, my work hours are over and I’m forbidden to work overtime

  • @Yes, leave a comment here on Monday

  • Good morning LINQ, we could continue with the help in question of the code?

  • @Yes, what’s the problem?

  • It was about the mistake in question of not being the same type when there was the request for the list

  • @OK, this is answered in my answer.

  • It is that I have some doubts yet, is that I have viewmodel classes, where are the classes that I use to interact with actions of the view and that would be necessary to create the new class or could use this?

  • @Guillotined You have some class that has exactly the same fields of this new?

  • Not with these names, but the same type of variable yes

  • @I’m in a bit of a hurry, you can join the chat?

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.