【问题标题】:ASP MVC multiple razor forms submits to same controller?ASP MVC 多个剃须刀表单提交到同一个控制器?
【发布时间】: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


【解决方案1】:

尝试在Html.BeginForm中指定参数名称

@using (Html.BeginForm(actionName: "Register", controllerName: "Account", method: FormMethod.Post, htmlAttributes: new { @class = "login", id = "f1" }))

另外我相信你有一个错字Html.BeginForm(Html.BeginForm

【讨论】:

  • 不工作,两种表格都是从同一个模型生成的,这可能是个问题吗?
  • 刚刚在一个新的 MVC 项目上尝试了这个场景,它工作得很好。你能发布路线设置吗?
猜你喜欢
  • 2013-11-08
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多