Link to home
Start Free TrialLog in
Avatar of wilfordrocks
wilfordrocks

asked on

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");
          }

    }
Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of wilfordrocks
wilfordrocks

ASKER

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>
...it sounds like you are saying to do this?
Yes.
Thant does not work.  Thanks anyway.
ASKER CERTIFIED SOLUTION
Avatar of wilfordrocks
wilfordrocks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My solution is simple and meets the need.  The other post lack too much detail to be helpful.
This is not an elegant solution, but sometimes a simple solution like this is best.