2

I need to build voting web site so, I have couple candidates and below them a vote button, how I can find which of the buttons was submitted

thanks

2 Answers 2

1

Give each of your buttons a name, like so (notice they are both "submit" buttons)::

<input type="submit" name="buttonYes" value="Yes" />
<input type="submit" name="buttonNo" value="No" />

Then, in your controller, capture a parameter for each of the two button names like this:

 public ActionResult Index(string buttonYes, string buttonNo) { ... }

You can then tell which button was pressed by checking to see which of these two parameters is not null; the one which is pressed with a have a value equal to the "value" attribute of the button, the other one will be null:

if (buttonYes != null)
{
    // Then the yes button was preseed
}
else if (buttonNo != null)
{
   // Then the no button was pressed
} 
else
{
    // Neither button was used to submit the form
    // and we got here some other way
}

The reason this works is because the web browser sends the information for the submit button that was pressed as part of the HTTP post to the web server. The button that was not pressed will not be sent with the post, and therefore the parameter will be null.

There are lots of ways to rewrite and optimzie this, but this is the essence of it and shows the fundamentals that are at work--you can play with it from there.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the solution, but I still have the problem , I just want the user to vote only for one candidate. therefore every buttons value are "vote" with different ID.
That works with the above--just change the value to each button to "Vote". The value doesn't matter in the solution above; it's the ID of the button that has to be different, exactly like what you are describing. And this solution only allows one vote for one candidate. Try it and see. You can also scale the solution up to an unknown number of candidates/vote buttons but just checking values inside the FormValues collection rather than parameters on the controller itself--that way you don't have to know what candidates to expect ahead of time.
Correction: That should say use a "FormCollection", like this: public ActionResult Index(FormCollection fc)
@Mike: Are you dynamically generating the list of Candidate Vote buttons in your view? Or are they static? I will do a code sample of a dynamic generation scenario if that's what you are doing.
@Mike: Good deal! Please make sure to vote up any answers that were helpful and accept an answer as correct if any helped you solve it--this will help other SO users who are searching for help on the same topic identify a solution for your question.
1

I wouldn't use the button value, I would set it up so that the url used to do the post encodes the vote itself. You could do this a couple of ways.

Use links

 <div class="left">
   <img src="/images/candidate/@Model.Candidates[0].ID" alt="@Model.Candidates[0].Name" />
   @Html.ActionLink( "Vote for " + Model.Candidates[0].Name, "count", "vote" )
 </div>
 <div class="right">
   <img src="/images/candidate/@Model.Candidates[1].ID" alt="@Model.Candidates[1].Name" />
   @Html.ActionLink( "Vote for " + Model.Candidates[1].Name, "count", "vote" )
 </div>

Use separate forms

 <div class="left">
   @using (Html.BeginForm( "count", "vote", new { id = Model.Candidates[0].ID } ))
   {
       <img src="/images/candidate/@Model.Candidates[0].ID" alt="@Model.Candidates[0].Name" />
       <input type="submit" value="Vote" />
   }
 </div>
 <div class="right">
   @using (Html.BeginForm( "count", "vote", new { id = Model.Candidates[1].ID } ))
   {
       <img src="/images/candidate/@Model.Candidates[1].ID" alt="@Model.Candidates[1].Name" />
       <input type="submit" value="Vote" />
   }
 </div>

Either of the above can be adapted to work with AJAX as well. Note, if you care, you'll need to build in some mechanism to detect vote fraud, e.g., add a one-time nonce to the url to verify that it isn't used more than once; track the number of times a user has voted if they are authenticated, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.