Sending post to Actionresult via knockout

Asked

Viewed 148 times

2

Speak people, next: I’m trying to send one post using the ko, for a method ActionResult of my controller instead of a JsonResult as usual. When debugging the project it enters the ActionResult but does not open the View which I am ordering. 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 just to take the data from model which come as a parameter to work with the same view EditarUsuario I don’t need to return anything. It has to do this with the JsonResult?

  • You want Ajax to transition the screen as well?

  • No no... Sent to Actionresult do not need to return anything via Json.

1 answer

1


Well, that’s not going to work. Um ActionResult moves the page request directly. Making the request by Ajax, what will be returned will be a View, which will be encapsulated by Ajax.

For this case, it is better to use a standard form even, or send to 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';
    }
});
  • 1

    I did what you said Gypsy... It worked. Vlws

Browser other questions tagged

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