Hello I am actually new to MVC
and i stuck with a problem. I tried and searched everywhere but the problem remain unsolved .
I am sending a model using $http
post to a MVC action
and to the view
. The data is successfully send over the action and view but the url isn't redirecting to the target view also in my case i want to send a model along with redirection .(I can perform redirection with windows.location.href="success"
after $http.success
but i can't send model data doing that)
Here is my sample try.(here i am providing user name as "random
," and password as "randPassword
" and i am sending a sample data(Name) to success view.
JS
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
var data = $.param({
UserName: $scope.user.username,
Password: $scope.user.password,
});
$http.post('Login', data, config).success(function (data, status, headers, config) {
}).then(function () {
console.log(data);
});
C# Action
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel usr)
{
if (usr.UserName=="random"&&usr.Password=="randPassword")
{
Session["Username"] = usr.UserName.ToString();
FormsAuthentication.SetAuthCookie(user.UserName, false);
return RedirectToAction("Success","Account",new {new {Name=usr.UserName}});
}
else
{
ModelState.AddModelError("", "UserName or Password is Wrong");
return View();
}
}
My success view is simple view with only success . The debug will continue to view on break-point too but without redirection . I can redirect with js after success but is things get complex when i want to send data and render a view . Thanks !!