0

I have a single form on my page with two ways of submitting it. The first is an anchor tag and the other is a submit button, both of them having different behaviours.

How can I assign to each a separate action method? Thanks.

1
  • 1
    You could use javascript to change the action method before submitting . Commented May 22, 2012 at 8:55

5 Answers 5

2

It really depends on what the anchor tag does - presumably it's triggering a submit in javascript?

If it's actually just doing a GET instead of a POST, then you can do as @dbaseman suggests - have separate action methods for the two request types.

But if the anchor does javascript submit, then my preference would be to simply give the submit button a name so you can detect it on the server in one action method, and then fork the code from there:

<submit name="fromButtom" value="Submit" />

And then your action method:

public ActionResult Foo(string fromButton)
{
  //if 'fromButton' contains 'Submit' then you assume it was the button.
}

Even better you can use a <button> instead, and then you can divorce the displayed text from the value that the button submits (useful if you're localising the page):

<button name="submitMethod" value="fromButton">Submit</button>

Now you can have a submitMethod parameter on your action method, in which you look for 'fromButton'.

Either way - the anchor tag/javascript (if that's how you're doing it) won't submit this value, because the button's value is only submitted when it's clicked.

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

1 Comment

Great, i can use this solution, however i'm using the value in the submit button as my display text and i dont want to rely on it in my code. i'm using a custom html helper as follows
1

Simply use the MVC HttpPost and HttpGet attributes on different versions of your action:

[HttpPost]
public ActionResult FormAction(Model model, string method = post) { ... }

[HttpGet]
public ActionResult FormAction(Model model) { ... }

3 Comments

You might want to change the method names here - that won't compile. You need different method names and an [ActionName] on there :)
@AndrasZoltan unless I'm losing it (granted entirely possible as it's 2am here), that should complie. What are you seeing as the problem?
lol - I've been there! Both methods have the same name and identical parameters; i.e. they are duplicates :)
0

You can use Url.Action or Html.ActionLink http://geekswithblogs.net/liammclennan/archive/2008/05/21/122298.aspx.

Comments

0

If the methods are absolutely diifferent then you should use JavaScript to make them act differently. With jQuery for instance, it would look like:

$("#sbutton").click(function() {
  alert("Submit button clicked!");
  // Do whatever you need - validate/initialize-fields/etc.
  return false; // This is required to prevent normal form submit
});

$("#slink").click(function() {
  alert("Link clicked!");
  // Do whatever you need - validate/initialize-fields/etc.
  return false; // This is required to prevent normal form submit
});

Comments

0

Just to add another solution, if you want to create something that will work with javascript switched off and it's not convenient to change button names and values then you can try this which I have used in production code.

Create a helper like this:

using System;
using System.Reflection;
using System.Web.Mvc;

namespace Whatever
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class MultipleSubmitAttribute : ActionNameSelectorAttribute
    {
        public string Name { get; set; }
        public string Argument { get; set; }

        public override bool IsValidName(ControllerContext controllerContext,
        string actionName, MethodInfo methodInfo)
        {
            bool isValidName = false;
            string keyValue = string.Format("{0}:{1}", Name, Argument);
            var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
            if (value != null)
            {
                value = new ValueProviderResult(Argument, Argument, null);
                controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
                isValidName = true;
            }

            return isValidName;
        }
    }
}

And now in your form you can do this sort of thing:

<input type="submit" name="action:DoSomething" value="Do something" /> 
<input type="submit" name="action:DoSomethingElse" value="Do something else" />

And then in your controller you can decorate your action method like this:

[HttpPost]
[MultipleSubmit(Name = "action", Argument = "DoSomething")]
public ActionResult DoSomething(ViewModel vm)
{ ... }

[HttpPost]
[MultipleSubmit(Name = "action", Argument = "DoSomethingElse")]
public ActionResult DoSomethingElse(ViewModel vm)
{ ... }

2 Comments

This solution would've worked great if i have two buttons instead of an <a> tag and a button
Yeah, I just thought it might come in handy for future use. Also, you can submit it from an <a> tag using javascript like this: onclick="$(this).parents('form').submit();return false;"

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.