0

I have created two razor forms in one view file in the view file i have created two Html.BeginForm() and mentioned different controllers, but when i submit the form, form submits to same controller.

view file

@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>
}

Controller File

[HttpPost]
[ValidateAntiForgeryToken]
    public ActionResult Login()
    {
        //Controller stuffs
    }

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register()
{
   //Controller stuffs
}

Each time when i submit the form it is going to register controller how to fix this?

Ahamed
  • 1
  • 3
  • Do it with @Ajax.BeginForm(). Check this [link](http://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/) how to work with Ajax.BeginForm – Basanta Matia Jul 07 '17 at 12:21
  • 1
    You have typo - `Html.BeginForm(Html.BeginForm` – Win Jul 07 '17 at 12:44
  • Inspect the markup and make sure that the boundaries of the form html tag are setup correctly when the client renders. – Brian Mains Jul 07 '17 at 14:00
  • Please go through this [Link] as you have this same condition(https://stackoverflow.com/questions/15788806/asp-net-mvc-4-multiple-post-via-different-forms) – Luqman Jul 14 '17 at 06:42

1 Answers1

0

Try specify the parameters name in Html.BeginForm

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

Also I believe you have a typo Html.BeginForm(Html.BeginForm