C# 无法隐式转换类型';布尔?&x27;至';布尔';
我想在boostrap identity 2.0注册表中添加一个复选框,以确保用户同意网站的条款和条件。由于某种原因,我得到了错误C# 无法隐式转换类型';布尔?&x27;至';布尔';,c#,twitter-bootstrap,asp.net-mvc-5,asp.net-identity-2,C#,Twitter Bootstrap,Asp.net Mvc 5,Asp.net Identity 2,我想在boostrap identity 2.0注册表中添加一个复选框,以确保用户同意网站的条款和条件。由于某种原因,我得到了错误无法隐式地将类型“bool”转换为“bool”,我不知道为什么 AccountViewModel.cs [Required] [Display(Name = "Agree Terms and Conditions")] public Nullable<bool> Agree { get; set; } #contact-form { position
无法隐式地将类型“bool”转换为“bool”
,我不知道为什么
AccountViewModel.cs
[Required]
[Display(Name = "Agree Terms and Conditions")]
public Nullable<bool> Agree { get; set; }
#contact-form {
position: relative;
z-index: 1;
margin: 0 0 20px;
padding: 6px 0 0 0;
vertical-align: top;
font-family: Arial, Helvetica, sans-serif;
}
#contact-form .error {
display: block;
overflow: hidden;
position: absolute;
height: 0px;
font-size: 10px;
color: #F00;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.txt-form {
display: block;
padding-bottom: 6px;
color: #ffffff;
}
.txt-form span {
color: #ffffff;
}
.form-div-1,
.form-div-2,
.form-div-3 {
float: left;
width: 183px;
}
.form-div-1 {
margin-right: 21px;
}
.form-div-2 {
margin-right: 21px;
}
#contact-form div {
overflow: hidden;
}
#contact-form fieldset {
position: relative;
z-index: 10;
overflow: hidden;
padding: 0;
width: 100%;
border: none;
}
#contact-form label .checkbox {
color:#15221E;
height:40px;
text-align:left;
width:324px
}
#contact-form select {
display: block !important;
margin-bottom: 2px;
width: 100%;
border: 1px solid #000;
background: #e1e3e2;
color: #acacac;
font-size: 13px;
line-height: 20px !important;
font-family: Arial, Helvetica, sans-serif;
cursor: pointer;
}
#contact-form select option {
padding-bottom: 3px;
}
#contact-form label {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
min-height: 60px;
width: 100%;
}
#contact-form .message {
display: block !important;
min-height: 251px;
width: 100%;
}
#contact-form input,
#contact-form textarea {
-webkit-box-shadow: inset 1px 0 3px rgba(0, 0, 0, 0.32);
-moz-box-shadow: inset 1px 0 3px rgba(0, 0, 0, 0.32);
box-shadow: inset 1px 0 3px rgba(0, 0, 0, 0.32);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
float: left;
margin: 0;
padding: 10px 10px 7px;
width: 100%;
outline: none;
border: none;
background: #e1e3e2;
color: #acacac;
font-size: 13px;
line-height: 20px !important;
font-family: Arial, Helvetica, sans-serif;
}
#contact-form input {
height: 37px;
}
#contact-form input:focus,
#contact-form textarea:focus {
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
}
#contact-form .area .error {
float: none;
}
#contact-form textarea {
overflow: auto;
width: 100%;
height: 287px;
resize: none;
}
#contact-form .success {
position: absolute;
top: 4px;
left: 0;
z-index: 20;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
display: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 14px 10px;
width: 100%;
border: 1px solid #000;
background: #e5412a;
color: #ffffff;
text-align: center;
text-transform: none;
font-size: 18px;
font-family: Arial, Helvetica, sans-serif;
line-height: 22px;
}
#contact-form .error,
#contact-form .empty {
position: absolute;
bottom: 1px;
left: 2px;
display: none;
overflow: hidden;
padding: 5px 4px 0px 0;
width: 100%;
color: #f00;
text-transform: none;
font-size: 11px;
line-height: 1.27em;
font-family: Arial, Helvetica, sans-serif;
}
#contact-form .message .error,
#contact-form .message .empty {
bottom: 5px;
}
.checkbox {
display: inline-block;
cursor: pointer;
font-size: 13px;
margin-right:40px; line-height:37px;
}
input[type=checkbox] {
display:none;
}
.checkbox:before {
content: "";
display: inline-block;
width: 37px;
height: 37px;
vertical-align:middle;
background: #e1e3e2;
color: #acacac;
text-align: center;
box-shadow: inset 0px 2px 3px 0px rgba(0, 0, 0, .3), 0px 1px 0px 0px rgba(255, 255, 255, .8);
border-radius: 3px;
}
input[type=checkbox]:checked + .checkbox:before {
content: "\2713";
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
font-size: 15px;
}
我认为复选框是真或假,布尔值也是如此。真或假是对的。它没有空状态,所以只需将其更改为直接bool
public bool Agree { get; set; }
这是CheckBoxFor的定义:
public static MvcHtmlString CheckBoxFor<TModel>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, bool>> expression,
IDictionary<string, Object> htmlAttributes
)
publicstaticmvchtmlstring CheckBoxFor(
这个HtmlHelper HtmlHelper,
表情表情,
辞典
)
第一个参数应返回
bool
而不是bool?
。不存在从bool
到bool?
的隐式转换。您需要使用类型转换。您的问题是您定义Agree
如下:
public Nullable<bool> Agree { get; set; }
编译器错误将被修复
编辑
在做出更改后,您声明您的复选框仍然没有显示。然后我要求你发布你的CSS,你做到了。以下是令人反感的风格:
input[type=checkbox] {
display:none;
}
删除该选项,将显示您的复选框。当我把你的代码放在小提琴上时,当我注释掉这种风格时,复选框就出现了。错误已经消失,但我没有看到复选框,但是如果我查看源代码,我会得到任何想法。?为什么你要在标签中放一个复选框?移除标签,看看发生了什么,只是一个空白区域,查看源代码显示
如果我将该查看源代码输出复制到JSFIDLE,它确实会显示一个复选框,您的css类form-div-5
中是否存在可能导致隐藏的内容?如果您仍然有问题,用附加信息更新您的问题。不鼓励在评论中进行对话。
public bool Agree { get; set; }
input[type=checkbox] {
display:none;
}