?
Solved

Handle multiple button in Mvc 4

Posted on 2016-07-25
3
Medium Priority
?
78 Views
Last Modified: 2016-07-25
Hello, I'm attempting to have multiple buttons submit to the same ActionResult; however, the btn value is always null. What am I doing wrong? What could cause this issue?

//view
 @using (Html.BeginForm())
{
  <button type = "submit" name = "btn" value = "save_new" id = "SaveNew" > Save & amp; New </button>
  <button type = "submit" name = "btn" value = "save_close" id = "SaveClose" > Save & amp; Close </button>
}


//controller
        [HttpPost]
        public ActionResult TestUsers(TestUsersModel model, string btn)

        {
            if (btn == "save_new")
            {
                // the form was submitted using the Save&New button
            }
            else if (btn == "save_close")
            {
                // the form was submitted using the Save&Close button
            }
            else
            {
                UpdateModel(model.Grid.UpdateParams);
                return Json(model.GetRows());
            }
}

Open in new window

0
Comment
Question by:Scott Baldridge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 41727807
I don't see anything wrong, and a quick test here works the way you expect it to work. Is it possible something in the model conflicts with the btn binding?
0
 

Author Comment

by:Scott Baldridge
ID: 41727847
It is working correctly now. There was another button in the view that does some JavaScript. I included that in the form and now it works. Don't know why that would matter...
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 41728057
That shouldn't matter, because elements outside the form are not sent to the server in HTTP POST. Could be some glitch that you corrected elsewhere. Anyway, glad you got it sorted out.
1

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question