我是MVC
的新手,并尝试将常规.NET Web应用程序转换为MVC
。
我正在登录页面上工作。
作为模板,我使用安装时附带的默认MVC
应用程序。
我将登录表单添加到索引页面,并将表单操作点更改为ActionController/Login
路由。
单击“提交”时,出现错误,指示无法找到资源/AccountController/Login
。
这是我的索引页面:
@model Admin.Models.LoginModel
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title</h1>
</hgroup>
</div>
</section>
}
<h3 class="logo">Title </h3>
<section id="loginForm">
@using (Html.BeginForm("Login","AccountController", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Please login</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
}
</section>
这是动作控制器的一部分:
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password))
{
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);
}
}
我在这里做错了什么?似乎一切都好。
答案 0 :(得分:1)
在您的视图中,使用以下内容替换Html.BeginForm:
@using(Html.BeginForm(“Login”,“Account”,FormMethod.Post))
答案 1 :(得分:0)
您需要使用[HttpGet]修饰您的登录操作,以表示它是一种GET方法。
编辑:同样,POST方法需要[HttpPost]