在控制器之间传递ViewModel

时间:2017-11-02 09:29:08

标签: c# asp.net-mvc asp.net-core asp.net-core-mvc

我正在尝试将split line charset ",.#;" 从一个ViewModel传递到另一个ControllerViewModel最初是正确创建的,但是当它传递给下一个Controller时,它为空。以下是ViewModel的创建方式:

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);
    }
}

正如我所说,LoginViewModel是使用我期望的属性创建的,但是当它被传递到HomeController并在Index.cshtml中处理时,我得到一个错误,指出{ {1}}为空。

0 个答案:

没有答案