Publish a form and redirect to an ASP.NET MVC action

I have a navigation bar that uses jQuery to move between the 4 steps of the registration process.

However, I need to make sure that everything works with disabled JS.

So, I have these 4 link images at the bottom of the page, and I need that if someone clicks, it goes to the current action, so I can save all the form data and then redirect to the next step.

Forwarding is quite simple, since I just pass the parameter in the route or form, but I don’t know how to send this method using only the action links.

I could add 4 different submit buttons with different classes for background images, etc., but that seems wrong.

Any ideas?

+6
asp.net-mvc
source share
3 answers

You may have several submit buttons in your form:

<input type="submit" name="step1" value="Step 1"/> <input type="submit" name="step2" value="Step 2"/> <input type="submit" name="step3" value="Step 3"/> 

and in your action:

 public ActionResult Action(FormCollection form) { if (!string.IsNullOrEmpty(form["step1"])) { // Step 1 button was clicked } else if (!string.IsNullOrEmpty(form["step2"])) { // Step 2 button was clicked } else if (!string.IsNullOrEmpty(form["step3"])) { // Step 3 button was clicked } ... } 
+2
source share

Without Javascript, a simple link cannot be sent via POST. To do this, you need to use the submit button. Fortunately, HTML provides an easy way to make an image submit button:

 <input type="image" src="http://url/to/image" alt="Step 1" id="btnStep1" /> 

You can always add jQuery processing for Javascript:

 $('#btnStep1').click(function(){...}); 
+3
source share

You can use the attribute that I found on net , which processes several buttons in the same form. This will determine what action is performed on the controller. Thus, you can have 4 actions on the controller, and the correct one, depending on which button was pressed, regardless of where it was called.

Such a small example; markup ...

 <input type="submit" name="action" value="step1"/> <input type="submit" name="action" value="step2"/> <input type="submit" name="action" value="step3"/> <input type="submit" name="action" value="step4"/> 

Then in the controller ...

  [HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "step1")] public ActionResult Step1(/* parameters */) { ... } [HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "step2")] public ActionResult Step2(/* parameters */) { ... } [HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "step3")] public ActionResult Step3(/* parameters */) { ... } [HttpPost] [MultiButton(MatchFormKey = "action", MatchFormValue = "step4")] public ActionResult Step4(/* parameters */) { ... } 

Then you can click between any steps in the registration process (perhaps after the check is completed and you will go through every first one) with relative ease.

Hope this helps someone. I just asked the date of the question, but thought that I would post this anyway :-)

+2
source share

All Articles