3

i have two options

One

<%= Html.CheckBox("postType", false, new { @id = item.int_PostTypeId.ToString() })%>

Second

<input type="checkbox"  name="postType" value="<%= item.int_PostTypeId.ToString() %>

1st question: what is differenct between these two method of check box declaration.

2nd question: and how can we get value of checked checkbox?

2 Answers 2

4

Use the Html.Checkbox (or even better the CheckBoxFor, in MVC2) if you use a model binder. It will handle the checkbox transparently to you, and you will read the boolean value on your model. It will render something more complicated than a checkbox (to avoid the problem which arises from the fact that an unchecked checkbox is not transmitted at all in a POST).

On the contrary, use the input if you retrieve the value via FormCollection: if you use the Html.Checkbox compound, you will receive something "strange" in the post (not the usual value, but a string with two testual values (like "true,false"). If you use the input, you just have to check if there exists that name in the post keys.

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

Comments

0

One major difference is that your are setting the "id" attribute of the HTML input (@ is used to denote the HTML tag attribute: @id, @class, etc.) in the first snippet and setting the "value" of the second...

You need to set the @id in both, so that you can retrieve the value easily in Javascript.

You didn't say in what context you wanted the value - whether client side via javascript or controller side via .NET.

2 Comments

your answer is not meaningful.
@Xulfee. I answered question 1 and asked a follow up question to question 2... It's not polite to lower my answer when my response was correct and to the point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.