0

i was looking for good trick to handle multiple submit button in form and then i got some advice from this url and i followed but fail. How do you handle multiple submit buttons in ASP.NET MVC Framework?

posted by @Andrey Shchekin.

he just said create a class like below one so i did in same controller

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

        if (!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
            return false;

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

then multiple submit button in the view look like & also controller code look like below

<% using (Html.BeginForm("Action", "Post")) { %>
  <!— …form fields… -->
  <input type="submit" name="saveDraft" value="Save Draft" />
  <input type="submit" name="publish" value="Publish" />
<% } %> 

and controller with two methods

public class PostController : Controller {
    [HttpParamAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SaveDraft(…) {
        //…
    }

    [HttpParamAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Publish(…) {
        //…
    } 
}

but when i test his code it never work. so any can tell me where i am making the mistake or code itself is wrong for handling the situation. thanks

Community
  • 1
  • 1
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

4

View:

<input type="submit" name="mySubmit" value="Save Draft" />
<input type="submit" name="mySubmit" value="Publish" />

Controller Action:

[HttpPost]
public ActionResult ActionName(ModelType model, string mySubmit)
{
   if(mySubmit == "Save Draft")
   {
      //save draft code here
   } else if(mySubmit == "Publish")
   {
      //publish code here
   }
}
Kaf
  • 33,101
  • 7
  • 58
  • 78
1

I had to deal with the similar scenario when I had the requirement that Users can finalize or save progress of the hospital infant record - essentially both actions are submit but one validates the record for insertion into the main DB table and another one saves it into a temp table without any validation. I handled it like this:

I have 2 buttons both are type submit with different IDs (btnSave and btnFinalize). When btnSave is clicked I intercept that event with some JQuery code:

$("#btnSave").click(function () {
   $("#SaveForm").validate().settings.rules = null;
   $('#SaveForm').attr('action', '@(Url.Content("~/Home/EditCase?finalize=false"))');
});

As you can see I modify the action attribute of the form to point to a different URL with a querystring attribute of finalize = false. I also remove any validation present on the model. If the other button is clicked I do nothing - executes the default behavior.

And in my controller I have a single action that handles both submit actions:

public ActionResult EditCase(EditInfantModel model, bool finalize = true)
{
    // Logic for handling submit in here...
}

I think you can apply the similar technique for your problem. I'm not sure if it's the answer you're looking for but I thought it was worth mentioning...

Marko
  • 12,543
  • 10
  • 48
  • 58