我有两个选项
一个
<%= Html.CheckBox("postType", false, new { @id = item.int_PostTypeId.ToString() })%>二
<input type="checkbox" name="postType" value="<%= item.int_PostTypeId.ToString() %>1号问题:什么是复选框声明的这两个方法之间differenct。
1st question: what is differenct between these two method of check box declaration.
第二个问题:我们怎么能得到选中的复选框的价值?
2nd question: and how can we get value of checked checkbox?
推荐答案如果你使用模型绑定使用Html.Checkbox(甚至更好的CheckBoxFor,在MVC2)。它会透明地处理复选框给你,你会读你的模型的布尔值。它将使东西比一个复选框更复杂(以避免产生于在后一个未经检查的复选框未发射的所有事实问题)。
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).
相反,使用输入,如果你通过的FormCollection检索值:如果使用Html.Checkbox复合,你会在后(不是一般的价值,但是有两个testual值的字符串收到一些奇怪 (如真,假)。如果你使用的输入,你只需要检查是否存在该名称在后按键。
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.
更多推荐
复选框asp.net的MVC
发布评论