Calling a page via ajax Angularjs

Asked

Viewed 77 times

0

My code ajax:

$scope.CarregaMenuPrincipal = function (data) 
{
    var req = $http({ method: 'POST', url: '/Home/Index',data:{usuario: data.usuario },})
       .then(function (response) {
           response;
       }, function (response) {
          response;
       });
}

My code in Controller

public ActionResult Index(UsuarioModel usuario)
{
    if (usuario != null && usuario.ListaRegras != null)
    {
        return View(usuario);
    }
    else
       return RedirectToAction("Login", "Home", usuario);
}

When running the Return View(user) line, it passes through the view to create the entire html screen but it stays on the login.cshtml screen and does not call the index.cshtmlscreen. Does anyone know how to redirect?

  • You need to redirect using Angularjs itself within your then().

  • I did this, but the Index action ta receiving value null $Scope.Log in = Function () { var usuario = $Scope.usuario; var req = $http({ method: 'POST', url: '/Home/Authenticate', date: { usuario: usuario }, }) . then(Function (Sponse) { $Scope.usuario.usuario = Sponse.data.dataUsuario; if (Sponse.data.msg == '') { window.location.href = '/Home/Index/' + { usuario: usuario.user };} Else {$window.Alert(Response.data.msg); } }, Function errorCallback(Response) { }); }

  • Your Action is receiving null because the Json you are passing is not in the correct format to map to UsuarioModel. Your model UsuarioModel has a property called usuario?

  • This way: public class User model { public string Id { get; set; } public string User { get; set; } public string Password { get; set; } public int Funcioid { get; set; } public string Functionname { get; set; } public List<Regrasuriomodel> Listrules { get; set; } }

No answers

Browser other questions tagged

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