[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

Handle multiple button in Mvc 4

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
Scott Baldridge
Asked:
Scott Baldridge
  • 2
1 Solution
 
PaulHewsCommented:
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
 
Scott BaldridgeAuthor Commented:
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
 
PaulHewsCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now