Does not list database data correctly in View Asp.net

Asked

Viewed 37 times

0

The data was entered into the database, but the View does not show one of the fields.

I checked in SQL Server and it’s all right, but the name field returns empty in view

public ActionResult Index()
{
    List<MovimentacaoVM> lispedido;
    using (Db db = new Db())
    {
        lispedido = db.Movimentacao.ToArray()
                      .OrderBy(x => x.MovimentacaoId)
                      .Select(x => new MovimentacaoVM(x)).ToList();
    }

    return View(lispedido);           
}

View code:

@model IEnumerable<PraticaNtoN.Models.MovimentacaoVM>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Criar")
</p>
<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.ProdutoNome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ClienteNome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataCriacao)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.ProdutoNome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ClienteNome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DataCriacao)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.MovimentacaoId }) |
            @Html.ActionLink("Details", "Details", new { id=item.MovimentacaoId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.MovimentacaoId })
        </td>
    </tr>
}

</table>

Method MovimentacaoVM

public MovimentacaoVM(Movimentacao row)
{
    MovimentacaoId = row.MovimentacaoId;
    ClienteId = row.ClienteId;
    DataCriacao = row.DataCriacao;
    ProdutoId = row.ProdutoId;
    ProdutoNome = row.ClienteNome;
    ProdutoNome = row.ProdutoNome;
}
  • You can post the code of view?

  • When you speak in "name field" you refer to Productname or Clientename?

  • to the clienteName all fields are filled in there in sql server manager, I checked, but when showing in the view comes all fields except the client name

  • Post also method code MovimentacaoVM

  • Dude, put the code to the method I was talking about up there.

1 answer

2


First of all, don’t sort the data in memory unless you have a reason for it. Of course this is not a recipe that you should follow blindly, but most of the time delegating ordering to the database is much more performative.

You can see more details about this at: When the Entity Framework executes the query of a Iqueryable?

public ActionResult Index()
{
    List<MovimentacaoVM> lispedido;
    using (Db db = new Db())
    {
        lispedido = db.Movimentacao.OrderBy(x => x.MovimentacaoId) //Remova o ToArray()
                      .Select(x => new MovimentacaoVM(x)).ToList();
    }

    return View(lispedido);           
}

Second, your mistake is very simple. See the method MovimentacaoVM, which is the method that selects items

public MovimentacaoVM(Movimentacao row)
{
    MovimentacaoId = row.MovimentacaoId;
    ClienteId = row.ClienteId;
    DataCriacao = row.DataCriacao;
    ProdutoId = row.ProdutoId;
    //ProdutoNome = row.ClienteNome;  // <----- Isso está errado
    ClienteNome = row.ClienteNome;  // <------- Deveria ser isto
    ProdutoNome = row.ProdutoNome;
}
  • My God as I have not seen it, what a shame hahahah.. Thank you, I’m making N for N . Now it’s working

Browser other questions tagged

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