MVC3 点击页面的注册按钮没反应,调试Controller中的Register也没有调试到
用MVC3实现的
点击页面的注册按钮没反应,调试Controller中的Register也没有调试到
- HTML code
<div class="row"> @Html.LabelFor(model => model.Department) @Html.EditorFor(model => model.Department) @Html.ValidationMessageFor(model => model.Department)</div><div class="row"> @Html.LabelFor(model => model.Telephone) @Html.EditorFor(model => model.Telephone) @Html.ValidationMessageFor(model => model.Telephone)</div><div class="row"> @Html.LabelFor(model => model.Mobile) @Html.EditorFor(model => model.Mobile) @Html.ValidationMessageFor(model => model.Mobile)</div><div class="row"> @Html.LabelFor(model => model.Address) @Html.EditorFor(model => model.Address) @Html.ValidationMessageFor(model => model.Address)</div><div class="row"> @Html.LabelFor(model => model.Desc) @Html.TextAreaFor(model => model.Desc) @Html.ValidationMessageFor(model => model.Desc) @Html.Hidden("Status",true)</div><div class="row"> <input type="submit" value="注册" /> <a href="javascript:history.go(-1)">返回</a> @*@Html.ActionLink("返回", "Index")*@</div>
- C# code
#region 注册用户 public ActionResult Register() { return View(); } [HttpPost] public ActionResult Register(FormCollection collection) { var user = new NalUser(); TryUpdateModel(user, collection); if (ModelState.IsValid) { acc.AddUser(user); acc.SaveChanges(); } return RedirectToAction("Index"); } #endregion
调试了可以看到,点击注册按钮,根本没跳到Controller里的Register方法。
[解决办法]
using (@Html.BeginForm("Register", "controller名字"))
{
<div class="row">
@Html.LabelFor(model => model.Department)
@Html.EditorFor(model => model.Department)
@Html.ValidationMessageFor(model => model.Department)
</div>
<div class="row">
@Html.LabelFor(model => model.Telephone)
@Html.EditorFor(model => model.Telephone)
@Html.ValidationMessageFor(model => model.Telephone)
</div>
<div class="row">
@Html.LabelFor(model => model.Mobile)
@Html.EditorFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile)
</div>
<div class="row">
@Html.LabelFor(model => model.Address)
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="row">
@Html.LabelFor(model => model.Desc)
@Html.TextAreaFor(model => model.Desc)
@Html.ValidationMessageFor(model => model.Desc)
@Html.Hidden("Status",true)
</div>
<div class="row">
<input type="submit" value="注册" />
<a href="javascript:history.go(-1)">返回</a>
@*@Html.ActionLink("返回", "Index")*@
</div>
}
[解决办法]
1. 你View的名字是否正确。
2. 确定你的注册按钮标签是不是在form表单里面。另外把form 标签的属性 method="post" 写上
3.可以试着把注册按钮弄成 button类型 ,用JQuery 提交表单 $("#表单id").submit(); 注意输入值要在表单标签里面。
[解决办法]