Asp.net MVC P2 中无法正确获取 CheckBox值的bug的解决方案

View:
<%=Html.CheckBox("IsBirthday","我是否过生日", item.IsBirthday)%>
Controller:
AUser user = new AUser();
BindingHelperExtensions.UpdateFrom(user, Request.Form);
Model:
public class AUser{
    
public bool IsBirthday{get;set;}
}

这样会发生无法从View中获取到IsBirthday值的情况
查了许多MS只能以以下方法解决
Controller:
AUser user = new AUser();
BindingHelperExtensions.UpdateFrom(user, Request.Form);
user.IsBirthday
= Request.Form["IsBirthday"]=="true";
View:
<%=Html.CheckBox("IsBirthday","我是否过生日","true", item.IsBirthday)%>


Over
同是学习,如果大家发现其它Bug欢迎交流.

posted @ 2008-04-28 09:06 重典 阅读(2378) 评论(4) 编辑 收藏

 回复 引用 查看   
#1楼2008-04-30 11:41 | 土星的狗狗      
取个CHECKBOX的值都这么麻烦,希望MVC快点发正式版啊~支持BZ继续~
 回复 引用 查看   
#2楼[楼主]2008-04-30 14:56 | 重典      
@土星的狗狗
毕竟是PV版的,错误会有很多
但所幸是MVC有开放的源代码,而且还有MonoRail做比较,所以有些自定义功能就容易实现多了

 回复 引用 查看   
#3楼2008-04-30 17:30 | 土星的狗狗      
@重典
刚开始学MONORAIL就出了ASP.NET MVC,不知道对我来说是不是件好事,呵呵~没有经历过元老级的!

 回复 引用 查看   
#4楼2008-05-04 14:32 | 膘汉      
将CheckBox的Value设置为“true”,然后将BindingHelperExtensions.cs的源代码

if (values[httpKey] != null) {
TypeConverter conv = TypeDescriptor.GetConverter(property.PropertyType);
object thisValue = values[httpKey];

if (conv.CanConvertFrom(typeof(string))) {
try {
thisValue = conv.ConvertFrom(values[httpKey]);
property.SetValue(value, thisValue, null);

}
catch (FormatException e) {
string message = property.Name + " is not a valid " + property.PropertyType.Name + "; " + e.Message;
if (ex == null)
ex = new PopulateTypeException("Errors occurred during object binding - review the LoadExceptions property of this exception for more details");

ExceptionInfo info = new ExceptionInfo();
info.AttemptedValue = thisValue;
info.PropertyName = property.Name;
info.ErrorMessage = message;

ex.LoadExceptions.Add(info);
}
}
else {
// TODO: Why do we throw an exception here instead of setting "ex"?
throw new FormatException("No type converter available for type: " + property.PropertyType);
}
}

修改为

if (values[httpKey] != null) {
TypeConverter conv = TypeDescriptor.GetConverter(property.PropertyType);
object thisValue = values[httpKey];

if (conv.CanConvertFrom(typeof(string))) {
try {
thisValue = conv.ConvertFrom(values[httpKey]);
property.SetValue(value, thisValue, null);

}
catch (FormatException e) {
string message = property.Name + " is not a valid " + property.PropertyType.Name + "; " + e.Message;
if (ex == null)
ex = new PopulateTypeException("Errors occurred during object binding - review the LoadExceptions property of this exception for more details");

ExceptionInfo info = new ExceptionInfo();
info.AttemptedValue = thisValue;
info.PropertyName = property.Name;
info.ErrorMessage = message;

ex.LoadExceptions.Add(info);
}
}
else {
// TODO: Why do we throw an exception here instead of setting "ex"?
throw new FormatException("No type converter available for type: " + property.PropertyType);
}
} else if (property.PropertyType == typeof(bool)) { // 当CheckBox没有被选中时将属性值设置为false
property.SetValue(value, false, null);
}

注意:当属性类型是枚举类型时CheckBox的值应该是枚举值之一。可以是字符串,当然也可以是整形。