POST model arriving null in controller

2

I have a complicated problem in my code that I can not solve.

Initially, I have a function SubmitUsuarioRegistro of JavaScript that performs the post of the user profile, forwarding the information to controller . This code was working normally:

function SubmitUsuarioRegistro() {
    var _queryString = $(#form-usuario).find('input, select').serialize();
    $.post('/Usuario/SalvarUsuarioPerfil', { objModel: _queryString }, function (resultado) {
        UsuarioPerfilSalvoValidacao(resultado);
    }).fail(function (ex) {
        notify('Não foi possível salvar seu perfil, entre em contato conosco!', 'danger');        
    });
}

And this is my controller :

[HttpPost]
public ActionResult SalvarUsuarioPerfil(UsuarioViewModel objModel)
{
    bool _bSucesso = false;
    int _idPk = 0;
    string xMsg = string.Empty;            

    //Template de salvar usuário.
    //Usado para separar as funcionalidades dentro da controller.            
    _cudTemplate = new SalvarUsuarioPerfilTemplate();
    try
    {
        var objInformacoesUser = UsuarioHelper.BuscaInformacoesUsuarioSession();
        var _retornoAux = _cudTemplate.SalvarUsuarioPerfil(objModel, objInformacoesUser.idAspNetUser, objInformacoesUser.idEmpresa);
        _bSucesso = _retornoAux.bSucesso;
        _idPk = _retornoAux.idRetorno;
        xMsg = _retornoAux.xMensagemRetorno;
    }
    catch (UnauthorizedAccessException ex)
    {
        return RedirectToAction("NaoAutorizado", "Account", new { xMensagemValidacao = ex.Message.ToString() });
    }
    catch (Exception ex)
    {
        if (ex.Message.ToString().Contains("login"))
        {
            return RedirectToAction("NaoAutorizado", "Account", new { xMensagemValidacao = ex.Message.ToString() });
        }

        xMsg = ex.Message.ToString();
    }


    return new JsonResult
    {
        Data = new
        {
            result = _bSucesso,
            mensagem = xMsg,
            primaryKey = _idPk
        },
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

After I gave the visual studio update (I do not really know if this has any relation or not), POST started to come with null on controller . Within JavaScript, it does the whole process of serialize correctly, but when it arrives at controller it arrives null .

I really do not know how to solve, does anyone know what it can be?

    
asked by anonymous 11.06.2018 / 04:43

1 answer

1

I've performed some procedures:

-Tried to use a $ .param that I saw through this post Post without form

This above, which apparently would be a fix, did not help me in the problem.

Fixed this problem was simply to remove the name of the model I was passing in $ .post

 $.post('/Usuario/SalvarUsuarioPerfil', _queryString , function (resultado)

I marked the answer because it solved my problem, but I did not understand the reason for this behavior between the view and the controller. If anyone has any thoughts on it, I'll leave it open during the week and end this post.

    
11.06.2018 / 13:40