4 Replies - 214 Views - Last Post: 15 February 2019 - 11:42 AM

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 250
  • Joined: 25-May 16

.NET MVC One Form Multiple Buttons

Posted 15 February 2019 - 06:42 AM

Hi everyone, I am working on an MVC page that already has a Save Button that writes to the database and redirects you to another page.

The button is called Save & Exit. I am trying to add a Save button where you stay on the same page.

I tried adding that button but the HttpPost takes me to the piece of code where it redirects you to another page. I tried giving both the buttons a name but have no idea how to bind it to the parameter of the function being called. Any tips on how to do that?

// Beginning of the form
@using (Html.BeginForm("Edit", "Controller", FormMethod.Post, new { id = "save-task-action" }))


// The Save and Exit button
<button name="submitButton" value="saveAndExit">Save &amp; Exit</button>


// The Save button
<button name="submitButton" value="save">Save</button>

// The Method in the controller
                [HttpPost, SkipSearch]
		public ActionResult Edit(int key, ActionViewModel viewModel)
		{
			try
			{
				var result = ModelBuilder.SaveTaskAction(key, viewModel);
				if (result.Validationerrors.Any())
				{
					result.Action = viewModel;

					return View(result);
				}
			}
			catch (DbEofException)
			{
				// Nothing to do here since we just want to redirect to the list anyway
			}

			return new RedirectResult(Url.Action("Task-List", "Controller"));
		}



Any insight on how I can add another button that stays on the same page?

I know I need to do something like
switch (button) // or whatever I name it
case "action1":
// do something
case "action2":
// do something else
default:
// do another thing



Is This A Good Question/Topic? 0
  • +

Replies To: .NET MVC One Form Multiple Buttons

#2 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: .NET MVC One Form Multiple Buttons

Posted 15 February 2019 - 07:07 AM

This is discussed here at SO. You should be able to access the value of the submit button either as

injected as a string parameter
from the FormCollection
or Request.Form

It won't actually stay on the same page, it will reload the page. You could consider Ajax if you don't want this reload cycle.
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 250
  • Joined: 25-May 16

Re: .NET MVC One Form Multiple Buttons

Posted 15 February 2019 - 08:06 AM

Maybe it's just me but I can't seem to get it to work. I added an additional parameter to get the string value of the button name

public ActionResult Edit(int key, ActionViewModel viewModel, string submitButton)




<input type="submit" name="submitButton" value="Save & Exit" />
<input type="submit" name="submitButton" value="Save" />




When I press the button, the controller is called as desired but the value of submitButton is null and I can't seem to find out why.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: .NET MVC One Form Multiple Buttons

Posted 15 February 2019 - 08:30 AM

Try it with just the single string submitButton parameter which, I'm guessing, will automatically be reconciled to the submit button. Adding more parameters will cause this association to fail. (Or perhaps just remove the int key parameter and test.)

Otherwise, just a single parameter for FormCollection, which will give you all the named inputs.

I'm basing this info on the page I linked, I haven't needed to do this myself that I can recall (or not for an age).
Was This Post Helpful? 0
  • +
  • -

#5 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 250
  • Joined: 25-May 16

Re: .NET MVC One Form Multiple Buttons

Posted 15 February 2019 - 11:42 AM

For now, I just bypassed it by doing something like this
 <button name="submitButton" id="submitButton" value="save">
Save                                    
</button>
@Html.Hidden("submitButton", "save");



Not ideal but gets the job done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1