Solved

Handle multiple button in Mvc 4

Posted on 2016-07-25
3
46 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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