POST model reaching null in controller

Asked

Viewed 197 times

2

I’m having a complicated problem with my code that I can’t seem to solve.

Initially, I have a function SubmitUsuarioRegistro of Javascript that carries out the post of the user’s profile, forwarding the information to the 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 mine 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 had given the update of the visual studio (I really don’t know if this is related or not), POST started coming null in controller. Within the Javascript it does the whole process of serialize correctly, but upon arriving at controller enough null.

I really don’t know how to fix it, someone knows what can be?

  • This request is coming to the Controller?

  • Arrives yes, it also makes the serialize with all the fields correctly, only it comes null

  • 1

    Sure your problem is in the type of data passed to your action. she expects a Salvarusuarioprofile(Usuarioviewmodel objModel) and you are sending a var _queryString object. is that comparable ? there are other ways to use jquery to make calling in the action more intuitive than the one you are using. https://stackoverflow.com/a/25068499/2740371

  • So, I was passing the name of the modeling on it, but when I withdrew the name and passed only _queryString, it worked, as I left in my reply. But until now I did not understand the reason, because it was working and suddenly stopped.

1 answer

1

I performed some procedures:

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

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

What fixed that problem was simply removing the name of the model I was spending on $.post

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

I marked it as an answer because it solved my problem, but I didn’t understand the reason for this behavior between the view and the controller. If anyone has any opinion on, I will leave it open during the week and finish this post.

Browser other questions tagged

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