2012-12-03 3 views
0

8 개의 다른 라디오 버튼 그룹 ('예/아니요'버튼 그룹)이있는 페이지가 있습니다. 원래는 '아니오'버튼을 선택하여 렌더링 할 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

양식을 '모델'데이터로 미리 채우고 있습니까? 만약 그렇다면, 그 값들이'false'로 설정되어 있지 않은지 확인하십시오. – Jack

답변

0

이 모델의 IsFelon 속성을 가지고 있는가 가치? 이것이 표준 부울이라면 그렇게 될 것입니다. 따라서 해당 속성을 값이 False 인 라디오 버튼에 매핑하면 MVC는 일치하는 것으로 인식하고 버튼을 checked으로 설정합니다.

내가 생각할 수있는 가장 쉬운 솔루션은 해당 라디오 버튼을 모델 속성에 매핑하지 않고 해당 값을 검색하고 컨트롤러에서 모델 속성을 프로그래밍 방식으로 다시 게시 할 때 설정하는 것입니다. IsFelon을 null 허용 속성으로 만들 수도 있지만 MVC에서 nullable 속성을 사용하면 주변을 둘러 볼 수 있습니다.

1

IsFelon을 초기화하지 않은 경우 기본적으로 개체의 부울 유형의 특성 인 "거짓"으로 설정됩니다.

한편, 라디오를 기본적으로 선택 해제하려면 bool을 사용하지 않아야합니다. "bool?" 디폴트 값을 "null"로 설정하여 아무런 라디오도 선택되지 않습니다.

관련 문제