【发布时间】:2017-07-07 11:48:54
【问题描述】:
我在视图文件的一个视图文件中创建了两个剃须刀表单我创建了两个Html.BeginForm() 并提到了不同的控制器,但是当我提交表单时,表单提交到同一个控制器。
查看文件
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.TextBoxFor(m => m.email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.email, "", new { @class = "text-danger" })
@Html.PasswordFor(m => m.password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.password, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
@using (Html.BeginForm(Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "login", id = "f1" })))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.ValidationSummary("", new { @class = "text-danger" })
<p class="form-row form-row-first input-required">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
@Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
@Html.PasswordFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.first_name, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.first_name, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.last_name, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.last_name, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.address, new { htmlAttributes = new { @class = "input-text" } })
@Html.ValidationMessageFor(model => model.address, "", new { @class = "text-danger" })
<div class="clear"></div>
<input type="submit" value="Signup" name="signup" class="button btn btn-default" />
<div class="clear"></div>
}
控制器文件
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login()
{
//Controller stuffs
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register()
{
//Controller stuffs
}
每次我提交表单时都会注册控制器如何解决这个问题?
【问题讨论】:
-
用@Ajax.BeginForm() 来做。检查此link 如何使用 Ajax.BeginForm
-
你有错字 -
Html.BeginForm(Html.BeginForm -
检查标记并确保在客户端呈现时正确设置了表单 html 标记的边界。
-
请通过这个[链接],因为你有同样的条件(stackoverflow.com/questions/15788806/…)
标签: asp.net asp.net-mvc razor asp.net-mvc-5