4
Follows the code:
View:
@model IEnumerable<Projeto.Models.Model>
<table class="table table-striped">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th width="15%">Ações</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
</tr>
}
</tbody>
</table>
<nav>
<ul class="pager">
<li><a href="#" onclick="pagina(-1)">Anterior</a></li>
<li><a href="#" onclick="pagina(1)">Próximo</a></li>
</ul>
</nav>
Controller:
using (var db = new Entities())
{
var resultado= (from p in db.Tabela
where p.Campo== 1
select p).ToList();
return View(resultado);
}
I tried to do it that way:
using (var db = new Entities())
{
var resultado = (from p in db.Tabela
where p.Campo == 1
select new SelectListItem
{
Value = p.descricao.Substring(0, 30)
}).ToList();
return View(resultado );
}
I get error:
The model item passed into the Dictionary is of type 'System.Collections.Generic.List
1[System.Web.Mvc.SelectListItem]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[Project.Models.Model]'.
Imagery:
As you can see from the image, the Id 60 and the description became very large, even made horizontal scroll bar. So I want to make substring.
In the first code works, only that the description got very large. I want to make substring before making return View()
That point before the equality operator is in its code?
p.Campo.== 1
, take it out, solve it? Because in the second, he used aValue
? And because the object needs to be oneSelectListItem
if at first you don’t need?– Maniero
What happens is your View expects a
IEnumerable
other than what you are returning in your action, post the code of your view to better help us.– Pablo Tondolo de Vargas
Maybe I don’t need Selectlistitem, make a loop of each description and substring(0, 15)
– Matheus Miranda