2016-08-24 2 views
1

체크 박스로 부울 값을 전달하고 싶습니다.체크 박스로 부울 값 전달

여기 내 재산

public bool MyBooleanValue{ get; set; } = true; 

그리고 여기, 내 HTML S : 내가 좋아하는 것이 기본에서

<input type="checkbox" id="@nameof(Model.MyBooleanValue)" name="my-boolean" value="1" class="" checked="checked" /> 

<label for="@nameof(Model.MyBooleanValue)">some text</label> 

<input type="hidden" name="my-boolean" value="true" /> 

이 선택 필드가 있습니다. 이 코드의 문제점은 무엇입니까? 나는 5 난 항상 같은 값 가장 간단한 방법은 MVC 도우미를 사용하는 것입니다

+0

'@ Html.CheckBoxFor (m => m.MyBooleanValue)'를 사용하십시오. –

답변

3

얻을 ASP.NET MVC를 사용

@Html.LabelFor(x => x.MyBooleanValue) 
@Html.CheckBoxFor(x => x.MyBooleanValue) 

당신은 확인할 수를 MyBooleanValueFormCollection 경우 true 또는 거짓 (확인란 상태에 따라 다름) 체크 박스가는 형태로 전달 될 값을 선택하면

<label for="MyBooleanValue">Some label text</label> 
<input type="checkbox" id="MyBooleanValue" name="MyBooleanValue" checked="checked" value="Some value you want to pass if checked"> 

:


는 또한 간단한 html 코드를 사용합니다. 그렇지 않으면 FormCollection에이 값이 없습니다.