2012-12-03 110 views
0

我有一個包含8個不同單選按鈕組的頁面('yes/no'按鈕組)。最初我已經編碼的HTML來呈現與'否'按鈕選中,但現在我需要渲染時沒有選擇按鈕。單選按鈕呈現爲'checked = checked',不確定源

我刪除了'checked = checked'並刷新了,但仍然選擇了NO按鈕。我再次刷新,清除緩存,並運行在不同的瀏覽器無濟於事。我正在尋找幫助尋找可能的原因。

我的MVC代碼:

@Html.RadioButtonFor(m => m.IsFelon, true, new { @class = "commentBox RadioIf" }) 
    <label for="PersonModel_Felon">Yes</label> 
@Html.RadioButtonFor(m => m.IsFelon, false, new { @class = "commentBox" }) 
    <label for="PersonModel_Felon">No</label> 

它是如何呈現:

<input class="commentBox" data-val="true" data-val-required="The ChildAbusePast field is required." id="ChildAbusePast" name="ChildAbusePast" type="radio" value="True" /> 
    <label for="HistoryModel_ChildAbusePast">Yes</Label> 
<input checked="checked" class="commentBox" id="ChildAbusePast" name="ChildAbusePast" type="radio" value="False" /> 
    <label for="HistoryModel_ChildAbusePast">No</Label> 

唯一的其他東西接觸,這是一些jQuery的:

$(document).ready(function() { 
     $("input.commentBox").click(function() { 
      if ($(this).closest('div.editor-field2').children('input:checked').val() == "True") { 
       $(this).closest('div.formQuestion').children('div.hidden').slideDown(800); 
      } else { 
       $(this).closest('div.formQuestion').children('div.hidden').slideUp("fast"); 
     } 
     }); 
    }); 
+0

您是否使用'Model'數據預填充表單?如果是,請檢查這些值是否設置爲「false」。 – Jack

回答

0

請問您的模型IsFelon財產有一個值?如果它是一個標準的布爾值,它會。因此,當您將該屬性映射到其值爲False的單選按鈕時,MVC會識別它們匹配並將按鈕設置爲checked

我能想到的最簡單的解決方案是不將這些單選按鈕映射到模型屬性,而只是檢索這些值並在回發中以編程方式在控制器中設置模型屬性。你也可以考慮使IsFelon爲空的屬性,但在MVC中使用可爲空的屬性可能涉及到一些擺弄。

1

如果您沒有初始化IsFelon,那麼默認情況下,它將被設置爲「False」,這是布爾型對象的性質。另一方面,如果你希望默認情況下取消選中收音機,那麼你不應該使用bool,你會想使用「bool?」。它將默認值設置爲「空」,因此將不會選擇任何無線電。

相關問題