MVC3 EditorTemplate for a nullable boolean using RadioButtons

前端 未结 6 1891
深忆病人
深忆病人 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条回答
  •  情书的邮戳
    2020-12-05 11:44

    You can use a @helper to simplify the accepted answer:

    @model bool?
    
    
    @Radio(true, "Yes", "Yes") @Radio(false, "No", "No") @Radio(null, "N/A", "NA")
    @helper Radio(bool? buttonValue, string buttonLabel, string buttonId) { Dictionary attrs = new Dictionary(); // Unique button id string id = ViewData.TemplateInfo.GetFullHtmlFieldId("") + buttonId; attrs.Add("id", id); // Check the active button if (Model == buttonValue) { attrs.Add("checked", "checked"); } @Html.RadioButtonFor(m => m, buttonValue, attrs) }

提交回复
热议问题