1

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.

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • Your cannot pass objects that contain properties which are complex objects or collections using `RedirectToAction()`. Just pass the ID of the object and get it again in the `HomeController` method –  Nov 02 '17 at 09:31
  • @StephenMuecke is there anyway to pass complex objects (`ViewModels`?) or is this simply just not the thing to do in ASP.NET? – CBreeze Nov 02 '17 at 09:32
  • 1
    Are you okay with using `TempData`? – Prajwal Nov 02 '17 at 09:33
  • You can only do it if the model contains simple properties (and even then you get a ugly query string and risk exceeding the query string limit and throwing an exception). Just pass the ID of the object, although its unclear why your doing this. –  Nov 02 '17 at 09:34
  • @StephenMuecke - say for example someone logs in as a user, and on the next page I want to display a message saying "Hello LoggedInUser", should I pass the user ID? – CBreeze Nov 02 '17 at 10:39
  • 2
    Yes, or just the value of `Email` - e.g. `return RedirectToAction("Index", "Home", new { email = user.Email);` to `public IActionResult Index(string email)` if thats the only property you need to pass to the `Index.cshtml` view –  Nov 02 '17 at 10:43
  • Read this excellent answer https://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction – Postlagerkarte Nov 02 '17 at 22:05

0 Answers0