2016-08-29 46 views
0

試圖做出多個提交表單,但有錯誤The current request for action 'EditAccount' on controller type 'AccountController is ambiguous between the following action methods。我怎樣才能解決這個問題?這裏的代碼多次提交表單在html中beginform mvc

屬性:

從控制器
public class HttpParamActionAttribute : ActionNameSelectorAttribute 
{ 
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) 
    { 
     if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase)) 
      return true; 

     var request = controllerContext.RequestContext.HttpContext.Request; 
     return request[methodInfo.Name] != null; 
    } 
} 

方法:

[HttpPost] 
[HttpParamAction] 
public virtual async Task<ActionResult> AddNew(AccountModel model) 
{ ... } 


[HttpPost] 
[HttpParamAction] 
public virtual async Task<ActionResult> EditAccount(AccountModel model) 
{ ... } 

,並查看

@using (Html.BeginForm(new {@class = "form-horizontal"})) 
{ 

    <button type="submit" class="btn btn-w-m btn-primary" name="EditAccount">Save</button> 

    <button type="submit" class="btn btn-w-m btn-primary" name="AddNew" ><i class="fa fa-plus"></i>&nbsp;Add New</button> 

} 
+1

我猜你不理解你的按鈕都在做你的方法參數是什麼,或者什麼(他們需要'爲了value'屬性後的值) 。建議你看看[這個問題/答案](http://stackoverflow.com/questions/30970159/net-mvc4-actionnameselectorattribute-multiple-buttons-within-view-is-not-workin) –

+1

我覺得這篇文章將有您的問題的答案:http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework – Monah

回答

2

試試這個,它會工作。根據namevalue調用該函數。

.cshtml

@using (Html.BeginForm("ActionName",new {@class = "form-horizontal"})) 
{ 

    <button type="submit" class="btn btn-w-m btn-primary" name="Account" value="Edit">Save</button> 

    <button type="submit" class="btn btn-w-m btn-primary" name="Account" value="AddNew" ><i class="fa fa-plus" ></i>&nbsp;Add New</button> 

} 

.cs

[HttpPost] 
[HttpParamAction] 
public virtual async Task<ActionResult> ActionName(AccountModel model, string Account) 
{ 
    if(Account=="Edit"){ 
    ... 
    } 
    else if(Account=="AddNew"){ 
    .... 
    } 
}