控制器: public class AccountController:Controller {
public ActionResult Index()
{
return View();
}
//
// GET: /Account/LogOn
public ActionResult UserLogin()
{
return View();
}
[HttpPost]
public ActionResult UserLogin(LogOn model)
{
if (ModelState.IsValid)
{
return View("Compltete", model);
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
查看:
@model SanjiviniAnalytics.Models.LogOn
<h2>User Login</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using(Html.BeginForm(“UserLogin”,“Account”,FormMethod.Post)) {
<div>
<fieldset>
<legend>Sign In</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<p>
<input type="submit" value="Log In" />
</p>
</fieldset>
</div>
}
型号:
public class LogOn
{
[Required]
[Display(Name="User Name")]
public string UserName { get; set; }
[Required]
[Display(Name="Password")]
[DataType(DataType.Password)]
public string Password { get; set;}
}
任何人都可以帮我找到我错的地方吗?
答案 0 :(得分:2)
从表单中取出Controller
.. MVC不要求您包含控制器名称的Controller部分:
@using (Html.BeginForm("UserLogin","Account"))
另外,尝试将POST指定为方法:
@using (Html.BeginForm("UserLogin", "Account", FormMethod.Post))
答案 1 :(得分:1)
试试这个:
@using (Html.BeginForm("UserLogin","Account", FormMethod.Post))
(看到西蒙打败了我:))
答案 2 :(得分:0)
正如西蒙所说,以下内容将起作用。 HttpPost操作将从FormMethod.Post方法执行。
@using (Html.BeginForm("UserLogin", "Account", FormMethod.Post))