Problems with types and conversions when filling a page

1

This is the error:

An exception of type 'System.InvalidCastException' occurred in App_Web_fjskumyk.dll but was not handled in user code

Additional information: Não é possível converter um objeto do tipo 'System.Collections.Generic.List'1[<>f__AnonymousType5'2[System.String,System.String]]' no tipo 'System.Collections.Generic.List'1[Ruptura.Models.MontaArvoreAcao]'.

I have tried everything and I still do not walk around in my code. Here's what I did in my Controller that returns the data:

var _listaUnidade = (
                             from r in db.Ruptura
                             join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
                             join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
                             where r.IDMotivo != 6

                             select new 
                             {
                                 a.Codigo_Unidade_Negocio,
                                 a.Unidade_Negocio
                             }).ToList().Distinct();

            ViewData["ListaUn"] = _listaUnidade.ToList();

Now at the moment of filling the page, it gives the stick. See my foreach in View, which has the problems already mentioned.

......
<ul>
                                    @foreach (var un in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaUn"])
                                    {  
                                        <li item-checked='false' item-expanded='false'>
                                            @un.Unidade_Negocio

                                        </li>
                                    }
                                </ul>
......

So I did not give the error any more, but the distinct did not work.

var _listaUnidade = (
                             from r in db.Ruptura
                             join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
                             join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
                             where r.IDMotivo != 6

                             select new MontaArvoreAcao
                             {
                                 Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
                                 Unidade_Negocio = a.Unidade_Negocio
                             }).ToList().Distinct();

            ViewData["ListaUn"] = _listaUnidade.ToList();

I tried to make a linq similar to this select (That works the distinct)

select distinct a.Unidade_Negocio, r.IDMotivo

from Ruptura r
join Apresentacao a on a.Codigo_Apresentacao = r.Codigo_Apresentacao
join Motivo m on r.IDMotivo = m.IDMotivo

group by r.IDMotivo,a.Unidade_Negocio

order by r.IDMotivo

In the following form I'm practically in the end, just a problem that I do not know where it comes from. For example, I have 5 Reasons listed. This is correct. For the IDMotivo (1,2 and 3) I have 3 UN (Dermocosmetics, MIP and Generic). He's just listing me 2 UN for each Reason. The reason of ID = 4, I have only two UN (Generic and MIP) and the Motive ID = 5 I have 3 UN (Generic, MIP and Dermocosmetic). It turns out that only 2 UN (Generic and MIP) is coming for everyone. Anything else missing? Here's how my code is.

Model:

public static List<MontaArvoreAcao> CriarListaArvoreAcao()
        {
            RupturaEntities db = new RupturaEntities();

              var _listaUnidade = (
                                       from r in db.Ruptura
                                       join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
                                       join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
                                       where r.IDMotivo != 6

                                       select new MontaArvoreAcao
                                       {
                                           Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
                                           Unidade_Negocio = a.Unidade_Negocio,
                                           IDMotivo = r.IDMotivo
                                       }

                                  ).ToList().Distinct().DistinctBy(d => d.Codigo_Unidade_Negocio).DistinctBy(s => s.IDMotivo);

            return _listaUnidade.ToList();
        }

My Controller:

public ActionResult Acao()
        {
            ViewData["ListaUn"] = MontaArvoreAcao.CriarListaArvoreAcao();

            return View(MontaArvoreAcao.montaArvoreAcao());
        }

My View:

<ul>
                    @foreach (var item in Model)
                    {
                        if (_motivo != @item.Motivo)
                        {
                            _idmotivo = @item.IDMotivo;
                            <li item-checked='false' item-expanded='false'>
                                @item.Motivo

                                <ul>
                                    @foreach (var un in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaUn"])
                                    {
                                        <li item-checked='false' item-expanded='false'>
                                            @un.Unidade_Negocio

                                        </li>
                                    }
                                </ul>
                              </li>
                            }
                            _motivo = @item.Motivo;
                        }
                </ul>
    
asked by anonymous 23.09.2014 / 15:55

2 answers

1

It is not possible to convert _listaUnit.ToList () into a list of type Ruptura.Models.MontaArvoreAcao .

  

An alternative:

Model Ruptura.Models.MontaArvoreAcao CreateAccountAction for example, which will receive the _list_Unity.ToList () and create your list of ActionAction.

That's probably because a list of Strings that you need to convert to the type you need List<Ruptura.Models.MontaArvoreAcao> is probably coming.

Following this reason, you would substitute:

ViewData["ListaUn"] = _listaUnidade.ToList();

By:

//O método CriarArvoreAcao recebe o retorno do seu método para montar a lista
ViewData["ListaUn"] = MontaArvoreAcao.CriarListaArvoreAcao(_listaUnidade.ToList());

In your Model class you would implement the method:

public class MontaArvoreAcao
{
   ...
   //Passe _listaUnidade.List() como parâmetro desse método
   public static List<MontaArvoreAcao> CriarListaArvoreAcao(...)
   {
      //Esse método receberia o tipo retornado por _listaUnidade.List()
      //como parâmetro e retornaria uma lista do tipo List<MontaArvoreAcao>
   }

   ...
}
  

Alternatively: Change your query by returning the type, in case MontaVilleAction .

Change where you were:

select new 
{
   Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
   Unidade_Negocio = a.Unidade_Negocio
}).ToList().Distinct();

To:

select new MontaArvoreAcao
{
   Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
   Unidade_Negocio = a.Unidade_Negocio
}).ToList().Distinct();

Tip 1: First of all it is not interesting to do this query that returns _listaUnit directly on your Controller.

The main idea of the MVC framework is to separate the different responsibilities, ie "Controllers" only interpret user actions and make calls to Model . The Model yes, it should perform this type of query, but I will not extend it, it's the tip.

Tip 2: In your "where r.IDMotivo! = 6" this number 6 can lead to future maintenance difficulties. Anyone who reads your code has no idea what it means.

    
23.09.2014 / 17:13
0

// Form 1 // Include at the beginning of your View a type for it: @model List

// In the controller: return View (MontaArvoreAcao.CriarListaArvoreAcao ())

// do not need the ViewData. in the view this query mode is read directly as: @foreach (var un in Model) {    //code }

// Form 2. // Include at the beginning of your View a type for it: @Remove model.Models.MontaAction

// In the model:  public List List {get; set; }

// In the controller:  MontaVerationAction model = new MontaVeration ();  model.List = model.CreateListAction ();  return View (model)

// do not need the ViewData. in the view this query mode is read directly as: @foreach (var un in Model.List) {    //code }

// Note: The word "Model" is reserved for the object in which it represents the type of its View //.

// Hope you have helped.

    
31.05.2017 / 15:12