I am trying to pass a ViewModel
from one Controller
to another. The ViewModel
is created correctly initially but when it is passed to the next Controller
it is null. Here is how the ViewModel
is created:
Login.cshtml
<div class="wrap">
<form action="/Login/LoginValidation" method="post">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input class="form-control" placeholder="Email Address" type="email" name="Email">
</div>
<div class="input-group">
<span class="input-group-addon "><span class="glyphicon glyphicon-lock"></span></span>
<input class="form-control" placeholder="Password" type="password" name="CorePassword">
</div>
<button class="btn-login" type="submit" value="Submit">Sign In</button>
</form>
</div>
LoginController
public class LoginController : Controller
{
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost] // Only call this if HttpPost is used
public IActionResult LoginValidation(Users user)
{
if (user != null)
{
if (LoginCheck(user))
{
var vm = new LoginViewModel
{
User = user
};
return RedirectToAction("Index", "Home", vm);
}
}
return null;
}
}
Index.cshtml
@model BootstrapProgram.ViewModels.LoginViewModel
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p>Hi there @Model.User.Email!</p>
</body>
</html>
HomeController
public class HomeController : Controller
{
public IActionResult Index(LoginViewModel vm)
{
return View(vm);
}
}
So as I said the LoginViewModel
is created with the properties I would expect, however when it is passed into the HomeController
and processed in Index.cshtml
I get an error stating that the @Model.User.Email
is null.