我想在我的主页上有一个登录表单。在index.cshtml
我有这个:
@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<div class="usernametb">
@Html.LabelFor(m => m.LoginMod.UserName)
</div>
<div class="usernametb">
@Html.TextBoxFor(m => m.LoginMod.UserName, new { style = "width:150px" })
@Html.ValidationMessageFor(m => m.LoginMod.UserName)
</div>
</div>
<div class="clear"></div>
<div>
<div class="pwtb">
@Html.LabelFor(m => m.LoginMod.Password)
</div>
<div class="pwtb">
@Html.PasswordFor(m => m.LoginMod.Password, new { style = "width:150px" })
@Html.ValidationMessageFor(m => m.LoginMod.Password)
</div>
</div>
<div class="clear"></div>
<div class="rememberme">
@Html.CheckBoxFor(m => m.LoginMod.RememberMe)
@Html.LabelFor(m => m.LoginMod.RememberMe, new { @class = "checkbox" })
</div>
<div><input class="loginbutton" type="submit" value="Log In" /></div>
}
我参考了这个模型:
namespace memsite.Models
{
public class MemEvent
{
public LoginModel LoginMod { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
我正在尝试点击此帐户控制器登录操作,但它从未点击过控制器,它只是重定向到/AccountController/Login
不存在的控制器。此外,验证控件不起作用...
namespace memsite.Controllers
{
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
(我是 MVC 的新手,永远离开了网络表单。)