1

I use the following HTML helper to generate check box:

<%= Html.CheckBox("DeluxeFeature")%>

Now I want to access the value of this checkbox in my controller. How should I do this? I am not going to use the method parameter name, for the reason that there are a lot of checkboxes and putting all of them in the parameter will clutter the method.

I try to use

Request.Form["DeluxeFeature"]

But the behavior is very weird; if the checkbox is not ticked, then the Request.Form["DeluxeFeature"] returns "false", which is expected. But if the checkbox is ticked, then it retruns "true, false".

Very weird, isn't it?

1
  • is there something else in your form named "DeluxeFeature"? Commented Jul 9, 2009 at 6:58

2 Answers 2

7

This thread on the asp.net forums explains the behavior - there's even a comment by Phil Haack from the ASP.NET MVC project team (bonus!!).

So the best way to handle it if you're not using the helpers/model binders as posted by levib seems to be

Request.Form.GetValues("DeluxeFeature")[0]
Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me.

var checkbox = Request.Form.Get("DeluxeFeature");
if (checkbox.Contains("true"))
{
    //Whatever code if the checkbox is checked.
}

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.