How to make Selectlistitem substring?

Asked

Viewed 78 times

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.List1[System.Web.Mvc.SelectListItem]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project.Models.Model]'.

Imagery:

inserir a descrição da imagem aqui

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 a Value? And because the object needs to be one SelectListItem if at first you don’t need?

  • 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.

  • Maybe I don’t need Selectlistitem, make a loop of each description and substring(0, 15)

1 answer

3


A quick solution to 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 );
  • I didn’t know you could do select new Projeto.Models.Model

  • Thanks, you helped out here.

Browser other questions tagged

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