复选框asp.net的MVC [英] CheckBox in asp.net mvc

查看:244
本文介绍了复选框asp.net的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选项

一个

<%= 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆