Sending post to ActionResult via knockout

2

Personal speech, following: I'm trying to send post using ko , to a ActionResult method of my controller instead of a JsonResult as usual. When debugging the project it goes into ActionResult but does not open to View that I'm requesting. Could someone help me?

My ko:

self.Edita = function (usuario) {
self.usuario(usuario);
$.ajax({
    url: '@Url.Action("EditarUsuario", "Administracao")',
    cache: false,
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: ko.toJSON(usuario),
    success: {}
});

};

My controller:

        [HttpPost]
    public ActionResult EditarUsuario(UsuarioViewModel usuarioViewModel)
    {
        return View();
    }

My idea is actually to just get the data from the model that comes as a parameter to work with it in view EditarUsuario I do not need to return anything. Can you do this with JsonResult ?

    
asked by anonymous 24.09.2015 / 00:38

1 answer

1

Well, that will not work. A ActionResult moves directly to the page request. By doing the request for Ajax, what will be returned will be View , which will be encapsulated by Ajax.

For this case, it is better to use a regular form, or to send Ajax a JsonResult and then redirect the page:

self.Edita = function (usuario) {
self.usuario(usuario);
$.ajax({
    url: '@Url.Action("EditarUsuario", "Administracao")',
    cache: false,
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: ko.toJSON(usuario),
    success: function (result) {
        window.location = '/Controller/MinhaAction';
    }
});
    
24.09.2015 / 01:06