How to return dynamic list?

Asked

Viewed 1,734 times

3

How to return a List<dynamic> in ASP.NET MVC 5 and Entityframework 6

For example I have an object with name User and mapped in my EDMX, when I do to bring the data dynamically like make an advanced query without returning an object of type User.

Note: this is a vwCaixa View

select top 5 SUM(vrLancamento),nmTipoDocumento from vwCaixa group by nmTipoDocumento

I did so:

public IList<dynamic> resumoCaixa()
        {
            IList<dynamic> lista = new List<dynamic>();

            TimeSpan time = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);



            using (var ctx = new dbProClinicEntities())
            {
               var t = ctx.vwCaixa.GroupBy(g => new { documento = g.nmTipoDocumento, pago = g.dtPago, valor = g.vrLancamento })
               .Select(x => new { pago = x.Key.pago, tipoDocumento = x.Key.documento, vrLancamento = x.Sum(f => f.vrLancamento) })
               .Take(5).ToList();

                foreach(var f in t)
                {
                    lista.Add(f);
                }
            }

            return lista.ToList();
        }

In Controller I am returning a Viewbag:

 public ActionResult Index()
    {
        AdminDAO dao = new AdminDAO();
        ViewBag.Caixa caixa = dao.resumoCaixa();
        return View();
    }

In View I’m doing so:

<table class="table list">
                <tbody>
                    @foreach(var caixa in ViewBag.Caixa)
                    { 
                    <tr>
                        <td>
                            <a href="#"><p class="title">@caixa.vrLancamento</p></a>
                            <p class="info">@caixa.tipoDocumento</p>
                        </td>
                   </tr>
                        }
                </tbody>
</table>

But it makes the following mistake:

inserir a descrição da imagem aqui

  • 2

    Are you in trouble? IN THESIS A return new List<dynamic>(); should work.

  • For example I have an object with name User and mapped in my EDMX, when I do to bring the data dynamically like make an advanced query without returning an object of type User

  • Already improved but still not understood what you want. Anyway edit the question and put all possible details. With what you initially put in, you couldn’t even guess what you really want.

  • 1

    I’ll put my code down for you to analyze minute.

  • 1

    You must [Dit] your question and not answer it.

  • 3

    And prefer to paste everything as text instead of image.

  • Sorry I’m new to the forum, but then took a look at my code? remembering that it is a View.

  • Okay buddy! blz...

  • I see here that the focus is more to post a standardized thing than to solve problems.

  • Remembering that it is a mapped view

  • I didn’t understand your placement could show me an example?

  • for such cases, the MVVM standard is used, with Viewmodel

Show 7 more comments

1 answer

2

In your example you are not passing the list as a model but the view bag. It is also not allowed to pass an anonymous class as a model to the view. Create a view model and use it in the Thumbnail query. Ex.:

var t = ctx.vwCaixa.GroupBy(g => new { documento = g.nmTipoDocumento, pago = g.dtPago, valor = g.vrLancamento }).Select(x => new **MINHAVIEWMODEL** { pago = x.Key.pago, tipoDocumento = x.Key.documento, vrLancamento = x.Sum(f => f.vrLancamento) }).Take(5).ToList();

and pass the variable t as a model using return View(t);

BS.: Don’t forget to put in the first line of the view @model IEnumerable<MINHAVIEWMODEL>

Browser other questions tagged

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