MVC Button passing value to handler

The entire view is 2 buttons and 2 div tags shown the values of 2 session variables.  The controller code (also given below) is just the GET and POST for the ActionResult Index.

My questions is this.  Each button has a value, 100 and 200 respectively, yet when the handler is called the value of "iValue" is always null.

Can I not pass the value of a button?  If not how can I determine which button called the controller?



index.cshtml - view code
--------------------------------
@{
    ViewBag.Title = "Index";
}

<h2>Index Switch test with button value pass back</h2>

<div style="color:red"> (THIS IS NOT WORKING) Value passed by button handler = @Session["ValuePassedToButtonHandler"]</div>
<div>(THIS IS WORKING) The switch changes with each button click = @Session["SwitchVar"]</div>
 
<form method="post">
<button name="CallIndexPost" type="submit" value="100">Call Home Controller ActionResult Index giving value 100</button>    
</form>

<form method="post">
<button name="CallIndexPost" type="submit" value="200">Call Home Controller ActionResult Index giving value 200</button>    
</form>


HomeController.cs - code
--------------------------------
 public class HomeController : Controller
    {
        static bool bSwitch = false;
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {

            Session["ValuePassedToButtonHandler"] = "Not Set";
            Session["SwitchVar"] = bSwitch.ToString();
            return View();
        }

        [HttpPost]
        public ActionResult Index(int? iValue)
        {
            bSwitch = !bSwitch;
            if (iValue != null)  // this value is always null, never 100 or 200??????
            {
                Session["ValuePassedToButtonHandler"] = iValue.ToString();
            }
            return this.RedirectToAction("Index");
          }

    }
wilfordrocksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
The parameter name in your action needs to match the name attribute of the thing you want to post. Change both of your buttons' name attributes to iValue. Or you could write your own model binder, but that would be more involved.
0
wilfordrocksAuthor Commented:
Sorry, not to be dim but it sounds like you are saying to do this?  Could you be more explicit, I have been using MVC for about 3 days now.

<form method="post">
<button name="iValue" type="submit" value="100">Call Home Controller ActionResult Index giving value 100</button>    
</form>

<form method="post">
<button name="iValue" type="submit" Value="200">Call Home Controller ActionResult Index giving value 200</button>    
</form>
0
käµfm³d 👽Commented:
...it sounds like you are saying to do this?
Yes.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

wilfordrocksAuthor Commented:
Thant does not work.  Thanks anyway.
0
wilfordrocksAuthor Commented:
This was the solution,
View:
<form method="post">
<button  type="submit" id="EnterprisePush" name="btnSubmit1" , value="Push">Call Home Controller ActionResult Index giving value 100</button>    
</form>

<form method="post">
<button  type="submit" id="EnterprisePull" name="btnSubmit2"  , Value="Pull">Call Home Controller ActionResult Index giving value 200</button>    
</form>

Controller
 public class HomeController : Controller
    {
        static bool bSwitch = false;
           
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            if (Session["ValuePassedToButtonHandler"] == null)
            {
                Session["ValuePassedToButtonHandler"] = "Not Set";
            }
         
            Session["SwitchVar"] = bSwitch.ToString();
            return View();
        }
         

        [HttpPost]
        public ActionResult Index(string btnSubmit1, string btnSubmit2)
        {
//where btnSubmit1 and btnSubmit2 are the name given in the view.
            bSwitch = !bSwitch;
            var button = btnSubmit1 ?? btnSubmit2;
            if (button != null)
            {
                Session["ValuePassedToButtonHandler"] = button.ToString();
            }
            return this.RedirectToAction("Index");
         
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wilfordrocksAuthor Commented:
My solution is simple and meets the need.  The other post lack too much detail to be helpful.
0
wilfordrocksAuthor Commented:
This is not an elegant solution, but sometimes a simple solution like this is best.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.