How to make substring SelectListItem?

4

Follow 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 this:

        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]'.

Image:

As you can see the image, the Id 60 and the description became very large, even made the horizontal scroll bar. So I want to do substring.

In the first code it works, except that the description is very large. I want to do substring before doing return View()

    
asked by anonymous 08.01.2017 / 19:32

1 answer

3

A quick fix for your case could be as follows

var resultado = (from p in db.Tabela
                          where p.Campo == 1
                          select new Projeto.Models.Model
                          {
                              Id = p.Id,
                              Description = p.descricao.Substring(0, 30)
                          }).ToList();
            return View(resultado );
    
08.01.2017 / 20:18