ASPNET Core - Error 500 when posting a method that returns a Partialview with only one object

Asked

Viewed 37 times

0

I have a method in my controller called ListagemTarefasEmDesenvolvimento() that returns me a PartialView. However, when I call this method via ajax, the server gives me a status error 500.

Below is my controller:

[HttpPost]
        public IActionResult ListagemTarefasEmDesenvolvimento()
        {
            var idUser = Int32.Parse(User.FindFirst("IdUsuario")?.Value);

            var obj = _tarefaContext.getTarefaEmDesenvolvimentoByIdUsuario(idUser).Result;
            
            var retorno = PartialView("_Em", obj);
            return retorno;
        }

The method getTarefaEmDesenvolvimentoByIdUsuario(), returns only one object. (does not return list).

This is my Partialview

    @model RKMES.Models.Tarefa
    <h6>Em Desenvolvimento</h6>
    <div class="panel panel-body panel-shadow border-top-verde overlayEmDesenvolvimento" style="padding:16px;padding-bottom:0; background-color:#ffffff">
    
        <div data-id="@Model.Id" class="row panel-shadow containerTarefasAFazer" style="margin-bottom:18px;padding:12px">
            <div>
                <div class="col-md-6">
                    <span style="font-size:16px"><strong>@Model.Nome</strong></span><br />
                    <span><strong>Atividade: </strong>@Model.Atividade.Nome</span><br />
                    <span><strong>Tempo previsto: </strong>@Model.HorasPrevisto</span>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <a style="float:right" onclick="PausarTarefa(@Model.Id)" class="btn btn-pausar btn-icon btn-rounded">
                            <i class="icon-pause2"></i>
                        </a>
                    </div>
                    <div class="row">
                        <a style="float:right" onclick="FinalizaTarefa(@Model.Id)" class="btn btn-roxo btn-icon btn-rounded">
                            <i class="icon-checkmark3"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

Some curiosities:

  • If I do the method getTarefaEmDesenvolvimentoByIdUsuario() return a list of objects, and I make an adjustment in the view, everything works normally.

  • From what I’ve seen in one of the building methods of PartialView(), he expects an object as a second parameter, so in theory, what I did should work normally but I get error 500.

Can anyone tell me where I’m going wrong?

  • Debug and see which is the error 500... only this information is not enough.

1 answer

0

Error occurs due to lack of validation of any of the arguments.

Consider that if the error is 500 the defect is not in that endpoint call.

The 500 family of errors refer to errors on the developer side, such as the lack of null validation leading to an Argumentnullexception. What your code should really do when User.FindFirst("IdUsuario")?.Value return null?

Just as a test measure, I suggest adding an error treatment by returning the internal error message. This will facilitate your diagnosis.

[HttpPost]
public IActionResult ListagemTarefasEmDesenvolvimento()
{
    try
    {
        var idUser = Int32.Parse(User.FindFirst("IdUsuario")?.Value);
        var obj = _tarefaContext.getTarefaEmDesenvolvimentoByIdUsuario(idUser).Result;
        var retorno = PartialView("_Em", obj);

        return retorno;
    }
    catch (Exception ex)
    {
        var retorno = Content(ex.Message);
        retorno.StatusCode = 500;
        return retorno;
    }
}

Then make a call to that endpoint via Swagger or Postman to see the error message.

Browser other questions tagged

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