2014-07-16 180 views
1

我想添加一個複選框到一個boostrap身份2.0註冊表單,以確保用戶同意網站的條款和條件。出於某種原因,我收到錯誤Cannot implicitly convert type 'bool?' to 'bool',我不知道爲什麼。不能隱式轉換類型'bool?' to'bool'

AccountViewModel.cs

[Required] 
[Display(Name = "Agree Terms and Conditions")] 
public Nullable<bool> Agree { get; set; } 

Register.cshtml

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @id = "contact-form", role = "form" })) 
{ 
<div class="form-div-5"> 
    <label> 
     @Html.CheckBoxFor(m => m.Agree, new { @checked = "checked" }); 
    </label> 
</div> 
} 

的site.css

#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; 
} 

我想到了一個checkb牛是真或假,所以是一個布爾值。

回答

0

真假是對的。沒有空狀態,所以只需將其更改爲簡單的布爾值即可。

public bool Agree { get; set; } 
1

這是CheckBoxFor的定義:

public static MvcHtmlString CheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, bool>> expression, 
    IDictionary<string, Object> htmlAttributes 
) 

的第一個參數,預計返回boolbool?。沒有隱含bool轉換爲bool?您需要使用類型轉換。

1

你的問題是你定義Agree這樣的:

public Nullable<bool> Agree { get; set; } 

定義Nullable<bool>的另一種方式是bool?,編譯器正確告訴你,沒有bool?bool之間可能存在的隱式轉換,因爲bool?類型的變量可以有一個空值,並且它怎麼能被映射到一個複選框?

切換到這一點:

public bool Agree { get; set; } 

,編譯器錯誤將被固定。

編輯

做出這樣的轉變後,你說你的複選框,並沒有顯示仍有上漲。然後我讓你發佈你的CSS,你做了。這裏是違規風格:

input[type=checkbox] { 
    display:none; 
} 

刪除,你的複選框將出現。當我將代碼放在小提琴中時,只要我註釋了該風格,就會顯示該複選框。

+0

錯誤已消失,但我沒有看到複選框,但是如果我查看源代碼,我會得到'

'任何想法? – iggyweb

+0

你爲什麼把一個複選框放在標籤內?刪除標籤,看看會發生什麼 –

+0

也嘗試過,只是一個空白區域,查看源顯示'

' – iggyweb

相關問題