2

I have a single form with multiple submit buttons that have same value. Example: "Proceed".

Now, in ASP.Net MVC's post controller method, how do I know which button is pressed?

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DepartmentDetails(string submitButton)

The value of submitButton will always be "Proceed". How do I know which button is pressed.

I have given separate IDs for each button.

Thanks.

2
  • You have two buttons with the same value but you want them to do different things? Won't this confuse the user? What is the problem with giving the buttons different values? Commented Dec 28, 2009 at 17:23
  • 2
    The ID's don't matter, the Name is what matters. Is that a different value? It sounds like there might be a simplier approach to what you're looking for. Providing some more detail might help. Commented Dec 28, 2009 at 17:30

3 Answers 3

4

Try this:

<% using (Html.BeginForm())
   { %>
   <input type="submit" value="Submit" name="Submit1Button" />
   <input type="submit" value="Submit" name="Submit2Button" />
<%} %>

public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection values)
    {
        string buttonName = values.AllKeys.Where((n) => n.EndsWith("Button")).SingleOrDefault();
        return View();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

2

Would it not make more sense to break your page into two different forms?

You can then use the arguments of your Html.BeginForm HtmlHelper method to specify different Controller(s) Action methods for each form.

Comments

0

I realise this question is ancient but just ran into it so thought I'd answer!

This is what we use:

/// <summary>
/// Attribute for Controller methods to decide whether a particular button
/// was clicked and hence whether the method can handle the action.
/// </summary>
public class IfButtonClickedAttribute : ActionMethodSelectorAttribute
{
    private readonly IEnumerable<string> _buttonNames;

    public IfButtonClickedAttribute(params string[] buttonNames)
    {
        _buttonNames = buttonNames;
    }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        if (controllerContext.HttpContext.Request.HttpMethod != "POST")
            return false;

        foreach (string buttonName in _buttonNames)
        {
            //this first test is for buttons or inputs that have the actual name specified
            if (controllerContext.HttpContext.Request.Form[buttonName] != null)
                return true;
        }

        return false;
    }
}

Then on your Actions you go:

[ActionName("SaveItem"), IfButtonClicked("SaveAsDraft")]
public ActionResult SaveAsDraft(){ ... }

[ActionName("SaveItem"), IfButtonClicked("SaveAsPublished")]
public ActionResult SaveAsPublished(){ ... }

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.