Solved

Handle multiple button in Mvc 4

Posted on 2016-07-25
3
39 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
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now