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.ICollection
1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController System.Web.Mvc.ActionResult Resultados(System.Collections.Generic.ICollection
1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController
yes, multiple submit buttons can include in the html form. One simple example is given below.
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.
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.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With