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?
– Marconi
Arrives yes, it also makes the serialize with all the fields correctly, only it comes null
– Paulo Garbelini
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
– Marco Souza
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.
– Paulo Garbelini