Problems with View Model Implementation

Asked

Viewed 99 times

1

I’m trying to use the concept of View Model Pattern, but when I’m implementing the compiler is showing a conversion error.

Below follows the approach I used:

Structure of the viewmodel

public class EvolucaoViewModel
{
    public Chamado ChamadoAtual { get; set; }
    public virtual ICollection<Evolucao> Evolucoes { get; set; }
}

Implementation in the controller

public ActionResult DetalharChamado(int? id)
{
    EvolucaoViewModel model = new EvolucaoViewModel();

    model.ChamadoAtual = _contexto.Chamados.Find(id);
    model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id);

    ViewBag.id_usuario = new SelectList(_contexto.Usuarios, "id_usuario", "nome_usuario", model.ChamadoAtual.id_usuario).OrderBy(p => p.Text);
    ViewBag.id_chamado = id;
    ViewBag.id_setor = new SelectList(_contexto.Setores, "id_setor", "nome_setor", model.ChamadoAtual.id_setor);
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    if (model == null)
    {
        return HttpNotFound();
    }
    return View(model);
}

When I try to do this assignment the VS presents me the message below:

model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id);

Error 3 Cannot implicitly Convert type 'System.Linq.Iqueryable' to 'System.Collections.Generic.Icollection'. An Explicit Conversion exists (are you Missing a cast?) E: Devspace Projetos Albasi.Atende Albasi.Atende.Web Controllers Chamadocontroller.Cs 77 31 Albasi.Atende.Web

What I need to modify?

  • It worked, young man?

  • Hello jbueno, for the controller yes, but for display in the view I only got through the Gridview helper. There is no way I can use Htmlhelper?

  • Marcelo, great then. But I have no idea about this new problem. By the way, it should be solved in another publication, okay? Open a new question with your current question, explaining your scenario and mark the answer that helped you most as correct using the on the left side of the answer (you can only mark one as correct).

3 answers

3

The estate Evolucoes of Viewmodel declares a ICollection and the return of the method Where (at least when applied to a IQueryable) is always a IQueryable, I mean, you’re trying to make a ICollection receive a IQueryable, that is wrong.

It is necessary to convert the return of Where for some kind that implements ICollection, as List or others.

What you need to change is

This line

model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id);

To

model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id).ToList();

1

Use the conversion to list ". Tolist()" in the excerpt below:

model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id).ToList();
  • Thanks jbueno and Elton. Situation partially resolved... I came across a new situation, this time in the view. There I could not use Htmlhelpers to display the information from the Evolutions collection. I could only do it using a Webgrid. How could I use something like @Html.Displaynamefor(model => model.Evolucoes.texto_evolucao) ???

  • Why did you just copy my answer, Elton?

  • @jbueno, I probably didn’t see your answer. Note that it is very simple, only a "Tolist" solves the cast problem and also the gap between mine and your publication was very small. ok?

  • Strange, it’s 57 minutes apart, but I believe you. I even asked because sometimes people who arrive new end up confusing some things...

  • @jbueno, care for quality and compliance with the rules is a virtue! Congratulations on your work in the community. If you think the answer is rethought, I’ll rule it out! what do you think?

  • Nah, it’s a single line of code and the solution is really this, it makes sense that we thought of the same thing. You can keep it and, as you’re right, I’ll leave a positive vote.

  • @jbueno, I left my vote on your ;)

Show 2 more comments

0

Try it this way:

model.ChamadoAtual = _contexto.Chamados.Include(i => i.Evolucoes).Find(id);

and removes the

model.Evolucoes = _contexto.Evolucoes.Where(e => e.id_chamado == id);
  • Trying this way the compiler presents the following error: Error 5 Cannot Convert lambda Expression to type 'string' because it is not a delegate type

  • Missing using System.Data.Entity; or you can also try: model.Calling = _context.Chamados.Include("Evolutions"). Find(id);

  • I’m already using using System.Data.Entity; With the suggested modification the compiler presents the following error: Error 4 'System.Data.Entity.Infrastructure.DbQuery<Albasi.Atende.Dominio.Entidades.Chamado>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Entity.Infrastructure.DbQuery<Albasi.Atende.Dominio.Entidades.Chamado>' could be found (are you Missing a using Directive or an Assembly Reference?)

Browser other questions tagged

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