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:
Are you in trouble? IN THESIS A
return new List<dynamic>();
should work.– Maniero
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
– Raniel Nogueira
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.
– Maniero
I’ll put my code down for you to analyze minute.
– Raniel Nogueira
You must [Dit] your question and not answer it.
– Maniero
And prefer to paste everything as text instead of image.
– Maniero
Sorry I’m new to the forum, but then took a look at my code? remembering that it is a View.
– Raniel Nogueira
Okay buddy! blz...
– Raniel Nogueira
I see here that the focus is more to post a standardized thing than to solve problems.
– Raniel Nogueira
Remembering that it is a mapped view
– Raniel Nogueira
I didn’t understand your placement could show me an example?
– Raniel Nogueira
for such cases, the MVVM standard is used, with Viewmodel
– Rod