MVC3 EditorTemplate for a nullable boolean using RadioButtons

前端 未结 6 1889
深忆病人
深忆病人 2020-12-05 10:55

I have a property on one of my objects that is a nullable boolean, I want my logic to have true represent Yes, false to be No and null to be N/A. Now because I am going to h

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 11:23

    @model bool?
    
    @{ Dictionary yesAttrs = new Dictionary(); Dictionary noAttrs = new Dictionary(); Dictionary nullAttrs = new Dictionary(); yesAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes"); noAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No"); nullAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "NA"); if (Model.HasValue && Model.Value) { yesAttrs.Add("checked", "checked"); } else if (Model.HasValue && !Model.Value) { noAttrs.Add("checked", "checked"); } else { nullAttrs.Add("checked", "checked"); } } @Html.RadioButtonFor(x => x, "true", yesAttrs) @Html.RadioButtonFor(x => x, "false", noAttrs) @Html.RadioButtonFor(x => x, "null", nullAttrs)

提交回复
热议问题