你只需使用这个:
// [HttpPost] and any other attributes necessary
public ActionResult YourAction(Login login)
{
...
}
但是考虑到您想要实现的逻辑的一般概念,我会说您不需要登录为空。
该操作只是做它做的事情:它处理登录(在客户端部分完成回发之后)。因此,它始终必须包含任何数据。
如果您想在对登录信息进行一些检查后重定向,您会收到它们,检查然后执行RedirectToAction(...)
或您想要的任何其他类型的重定向。
然后在视图中您将拥有(例如,对于索引操作):
@model Login
@using(Html.BeginForm("YourAction", "home"))
{
@Html.LabelFor(model => model.UserName)
@Html.TextBoxFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
@Html.LabelFor(model => model.Password)
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
<input type="submit" value="go"/>
}
编辑:
想象一下您的 YourAction 是 Login:
[HttpGet]
public ActionResult Login()
{
// this one is called on the first entering of login and shows the form to fill
return View(new Login()); // for instance
}
[HttpPost]
public ActionResult Login(Login login)
{
// this one is called on PostBack after filling the form and pressing 'Login' (or something) button
// todo: here you to validate the login info
// and then either do your redirect to some other page or showing the login form again
// if(something)
// return RedirectToAction(...);
return View(login);
}