How to return warning in a View if there is no data to display?

1

I've created the code below that generates a list. The same check whether the list contains any data recorded in it or not. If it has data, it returns a View with the list inside, if it does not have data, it returns null .

How to do, instead of returning null , display a warning on the page itself stating that there is no data to display?

[HttpPost]
    public ActionResult Relatorio(string ocupacaoId, string instrumentoRegistroId)
    {
        List<ProcedimentoOcupacaoRegistro> listaprocedimentoocupacaoregistro = new ProcedimentoOcupacaoRegistro().listaProcedimentoOcupacaoRegistro(ocupacaoId, instrumentoRegistroId);
        ViewBag.ocupacaoId = ocupacaoId;
        ViewBag.instrumentoregistroId = instrumentoRegistroId;
        if (listaprocedimentoocupacaoregistro.Count > 0)
        {
            return View(listaprocedimentoocupacaoregistro)
        }
        else
        {
            return null; // <<---AQUI EU QUERO ALTERAR
        }
    }
    
asked by anonymous 22.06.2017 / 22:53

1 answer

2

I suppose you want this:

return new EmptyResult();

Documentation .

The documentation of class ActionResult shows all types return to the view . You can even create more that suit your needs.

There are other ways.

From what I've seen null ends up giving the same result.

Then you have to deal with view to deal with this situation. You can send it to the normal view and treat it as present to the user, something like this:

@model IEnumerable<ProcedimentoOcupacaoRegistro>

@if (Model.Count() > 0) {
    ...
} else {
    <div>Não tem itens pra mostrar</div>
}

Or, what is more common, send a different view that shows the error message you want, after all show the data is a view, show that has no data to show is another view , example:

return RedirectToAction("Error", "Relatorio");

Depending on how it is mounted you can send code:

return JavaScript( "alert('Nada a mostrar');" );

Or:

return View("SemDadosView");

Of course you need to create view the way you want it.

    
22.06.2017 / 22:57