0

我正在修改一个现有的 asp.net mvc 应用程序,该应用程序从模型上的列表中创建一个复选框列表,属性名称为“MyModelProperty”,并另​​外为“全选”生成一个具有以下 html 的输入元素:

<input name="MyModelProperty_SelectAll" type="checkbox" CHECKED="checked" value=""/>

模型中的属性声明将创建一个布尔属性,该属性将绑定到视图中的现有元素?

我试过,'public bool MyModelProperty_SelectAll {get;set;}' 但它返回 null。那是因为值是 html 输入控件中的空字符串吗?

4

2 回答 2

2

更改字符串Model的属性:

public string MyModelProperty_SelectAll { get; set; }

checkbox为, 然后在服务器中设置一些值,如果选中,则该将是给定的value,否则您将看到null

<input name="MyModelProperty_SelectAll" type="checkbox" value="all"/>

编辑:

如果要将其绑定到布尔值,则必须提供一个value="true"和一个隐藏字段:

<input class="input-validation-error" id="MyModelProperty_SelectAll" name="MyModelProperty_SelectAll" type="checkbox" value="true">
<input name="MyModelProperty_SelectAll" type="hidden" value="false">

上面的示例代码是使用Html.CheckBox帮助程序生成的。

于 2013-10-18T17:20:44.570 回答
2

如果您不使用 MVC htmlhelpers 生成复选框,则必须为您的复选框添加一个额外的隐藏元素:

<input name="MyModelProperty_SelectAll" type="checkbox" value="true"/>
<input name="MyModelProperty_SelectAll" type="hidden" value="true"/>
于 2013-10-18T17:31:43.517 回答