Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 4 Form with 2 submit buttons/actions

Tags:

asp.net

razor

I have a form in ASP.Net and razor.

I need to have two ways of submitting said form: one that goes through the Edit action, and another that goes through the Validate action.

How should I go about doing this?

I don't mind using JavaScript for this.

EDIT:

Using the custom attribute I get this error.

The current request for action 'Resultados' on controller type 'InspecoesController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Validar(System.Collections.Generic.ICollection1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController System.Web.Mvc.ActionResult Resultados(System.Collections.Generic.ICollection1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController

like image 865
Tiago Veloso Avatar asked May 10 '13 10:05

Tiago Veloso


People also ask

Can a form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do I apply multiple submit buttons in one form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Which function is used to handle multiple submit buttons?

Multiple buttons with different names To display a data entry textboxes EditorForModel() helper is used. You can very well use helpers such as TextBoxFor() and LabelFor() if you so wish. There are two submit buttons - one with name attribute set to save and the other with name of cancel.


2 Answers

That's what we have in our applications:
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;     } } 

Actions decorated with it:

 [HttpParamAction] public ActionResult Save(MyModel model) {     // ... }  [HttpParamAction] public ActionResult Publish(MyModel model) {     // ... } 

HTML/Razor

@using (@Html.BeginForm()) {     <!-- form content here -->     <input type="submit" name="Save" value="Save" />     <input type="submit" name="Publish" value="Publish" /> } 

name attribute of submit button should match action/method name

This way you do not have to hard-code urls in javascript

like image 117
Ramunas Avatar answered Oct 02 '22 10:10

Ramunas


You can do it with jquery, just put two methods to submit for to diffrent urls, for example with this form:

<form id="myForm">     <%-- form data inputs here ---%>     <button id="edit">Edit</button>     <button id="validate">Validate</button> </form> 

you can use this script (make sure it is located in the View, in order to use the Url.Action attribute):

<script type="text/javascript">       $("#edit").click(function() {           var form = $("form#myForm");           form.attr("action", "@Url.Action("Edit","MyController")");           form.submit();       });        $("#validate").click(function() {           var form = $("form#myForm");           form.attr("action", "@Url.Action("Validate","MyController")");           form.submit();       }); </script> 
like image 44
Łukasz W. Avatar answered Oct 02 '22 10:10

Łukasz W.