0

Trying to make multiple submit form, but having error The current request for action 'EditAccount' on controller type 'AccountController is ambiguous between the following action methods. How can i fix this? Here's code

Attribute:

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

Methods from controller:

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


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

And view

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

}
2
  • 1
    I guessing your not understanding what your method parameters are or what your buttons are doing (and they need value attributes in order to post a value). Suggest you look at this question/answer Commented Aug 29, 2016 at 12:30
  • 1
    I think this article will have the answer for your question: stackoverflow.com/questions/442704/… Commented Aug 29, 2016 at 13:09

1 Answer 1

3

Try this, It will work. Basis on name and value call the function.

.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"){
    ....
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.