Reputation: 207
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.
Upvotes: 0
Views: 1487
Reputation: 14951
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)
{ ... }
Upvotes: 0
Reputation: 42363
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.
Upvotes: 2
Reputation: 102783
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) { ... }
Upvotes: 1
Reputation: 135
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
});
Upvotes: 0
Reputation: 923
You can use Url.Action or Html.ActionLink http://geekswithblogs.net/liammclennan/archive/2008/05/21/122298.aspx.
Upvotes: 0