How to show a message on the screen if there are no records to be brought from the database?

Asked

Viewed 58 times

0

I want to show a message on the screen instead of showing an empty table. How do I implement this logic in index.cshtml?

inserir a descrição da imagem aqui

@page
@model xxx.xxx.Pages.Servicos.IndexModel

@{
    ViewData["Title"] = "Serviços";
}

<h2>Serviços</h2>

<p>
    @{
        if (await Model.PermiteIncluirAsync())
        {
            <a asp-page="Create">Novo serviço</a>
        }
    }
</p>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].Descricao)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].UnidadeDeMedida)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].Grupo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].Subgrupo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].ValorUnitario)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].CargoResponsavel)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Servicos[0].Observacoes)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.Servicos) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Descricao)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnidadeDeMedida)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Grupo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Subgrupo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ValorUnitario)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CargoResponsavel)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Observacoes)
            </td>
            <td>
                @{
                    if (await Model.PermiteAlterarAsync())
                    {
                        <a class="btn btn-default" asp-page="./Edit" asp-route-id="@item.Id">Editar</a>
                    }

                    if (await Model.PermiteVisualizarAsync())
                    {
                        <a class="btn btn-default" asp-page="./Details" asp-route-id="@item.Id">Detalhes</a>
                    }

                    if (await Model.PermiteExcluirAsync())
                    {
                        <a class="btn btn-default" asp-page="./Delete" asp-route-id="@item.Id">Excluir</a>
                    }
                }
            </td>
        </tr>
    }
    </tbody>
</table>

  • Make an if (Model.Service?.Length > 0) around your table and put in ELSE what you want to show.

1 answer

1


You can use the if to check the amount of records:

@if (model.Servicos.Count == 0)
{
   // mensagem que você quer
}
else
{
   // o código com resgistro
}   

Browser other questions tagged

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