【问题标题】:ASP.NET mvc angularjs post does not redirectASP.NET mvc angularjs 帖子不重定向
【发布时间】:2016-05-08 20:35:26
【问题描述】:

为什么当我向Login 发出帖子请求时,RedirectToAction 不起作用并且 url 没有更改为 /Home/Second

AngularJS:

$scope.answer = function(answer) {
  $mdDialog.hide(answer);
  $http({
      method: 'POST',
      url: 'Home/Login',
      data: JSON.stringify({ email: $scope.user.email, password: $scope.user.password })
  });
};

家庭控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }
    [HttpGet]
    public ActionResult Second()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(string email,string password)
    {
        return RedirectToAction("Second");
    }
}

【问题讨论】:

    标签: javascript asp.net angularjs asp.net-mvc


    【解决方案1】:

    您可以从下面的 Angular js 代码中重定向它,而不是从控制器重定向它;

    你的 Js 代码将是

     $scope.answer = function(answer) {
              $mdDialog.hide(answer);
              var response = $http({
                  method: 'POST',
                  url: 'Home/Login',
                  data: JSON.stringify({ email: $scope.user.email, password: $scope.user.password })
              });
    
           if(response == "Success")
           {
              $window.location.href = "/Home/Second";
           }
    
        });
    

    在你的控制器中你必须像这样编写你的方法:

     public string Login(string email,string password)
     {
            return "Success";
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2011-06-25
      相关资源
      最近更新 更多