2017-07-07 44 views
0

我在視圖文件中的一個視圖文件中創建了兩個剃刀形式,我創建了兩個Html.BeginForm()並提到了不同的控制器,但是當我提交表單時,表單提交給相同的控制器。ASP MVC多個剃鬚刀表單提交給相同的控制器?

視圖文件

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

當我提交它要註冊控制器如何解決這個問題的形式每一次?

+0

做它用@ Ajax.BeginForm()。檢查此[鏈接](http://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/)如何使用Ajax.BeginForm –

+1

你有錯字 - ' Html.BeginForm(Html.BeginForm' – Win

+0

檢查標記並確保在客戶端呈現時,表單html標記的邊界安裝正確。 –

回答

0

嘗試在Html.BeginForm

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

指定的參數的名稱此外,我相信你有一個錯字Html.BeginForm(Html.BeginForm

+0

無法正常工作,兩種形式都是從同一個模型生成的,因此可能會出現問題? – Ahamed

+0

剛試過這個場景在一個新的MVC項目上,它工作得很好,你可以發佈路線設置嗎? –