0
I’m trying to perform the query below:
var query = from s in db.Crm_Analise
where s.cliente_CRM == cod_cli
group s by s.TAG into g
select new
{
TAG = g.Key,
ATUALIZAÇÃO = g.Max(t => t.data_creat),
RELATÓRIOS = g.Count(t => t.modelo != null)
};
However, the error is presented:
Follows the View
:
@model IEnumerable<OneeWeb_v3.Models.Crm_Analise>
@{
ViewBag.title = "TAG's";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h2>@ViewBag.title</h2>
<table class="table">
<tr>
<th>
@Html.ActionLink("TAG", "Index")
</th>
<th>
@Html.ActionLink("ATUALIZAÇÃO", "Index")
</th>
<th>
@Html.ActionLink("RELATÓRIOS", "Index")
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TAG)
</td>
<td>
@Html.DisplayFor(modelItem => item.data_creat)
</td>
<td>
@Html.DisplayFor(modelItem => item.modelo)
</td>
<td>
@Html.ActionLink("Visualizar", "Details")
</td>
</tr>
}
</table>
<ul class="pager">
<li><a href="#">Anterior</a></li>
<li><a href="#">Proximo</a></li>
</ul>
Model:
public class Crm_Analise
{
[Key]
public int id { get; set; }
public string cod_item_CRM { get; set; }
public string TAG { get; set; }
public string data_creat { get; set; }
public string modelo { get; set; }
public int cliente_CRM { get; set; }
}
Controller:
// GET: Crm_Analise
[Authorize]
public async Task<ActionResult> Index()
{
if (Session["cod_cli"] != null)
{
int cod_cli = Convert.ToInt32(Session["cod_cli"]);
var query = from s in db.Crm_Analise
where s.cliente_CRM == cod_cli
select new
{
TAG = s.TAG,
ATUALIZAÇÃO = s.data_creat,
RELATÓRIOS = s.cod_item_CRM
};
if (query.Count() > 0)
{
return View(await query.ToListAsync());
}
else
{
return RedirectToAction("Index", "Home", new { cod_cli = Session["cod_cli"], razao_social = Session["razao_social"] });
}
}
else
{
return RedirectToAction("Login", "Account");
}
}
I know it has something to do with @model IEnumerable<OneeWeb_v3.Models.Crm_Analise>
but I don’t know how to solve..
the type sent to View is different from what is in your configuration, you need to create a
ViewModel
when that’s how– novic
@Virgilionovic Thanks. I will search on viewmodel
– Thomas Erich Pimentel
Dude, put the method that you encapsulate to the screen object and send the controller as well to help... Probably you are sending an object of type "A", but on your screen the "@model" expects the type B.
– Ayrton Giffoni
Example: https://answall.com/questions/169742/onde-coloca-a-regra-da-view-model/169761#169761
– novic
@Ayrtongiffoni Update made. thank you
– Thomas Erich Pimentel