MVC3 EditorTemplate for a nullable boolean using RadioButtons

前端 未结 6 1871
深忆病人
深忆病人 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
    2020-12-05 11:45

    using Canvas example above I built a customization model and view so you can control the values via a model and edit them in code, bools aren't always a yes/no/(n/a) so Here's how it looks in MVC5.

    using a generic model for the nullable bool

    public class Customisable_NullableRadioBool
        {
            public bool? Result { get; set; }
            public string TrueLabel { get; set; }
            public string FalseLabel { get; set; }
            public string NullLabel { get; set; }
            public string AttributeTitle { get; set; }
        }
    

    Here's the CSHTML to be stored in: ~/Views/Shared/EditorTemplates/Customisable_NullableRadioBool.cshtml

    @model Customisable_NullableRadioBool
    @Model.AttributeTitle


    And then you can reference the generic class and the editor template through the rest of your project and render the editor template like this.

    @Html.EditorFor(m => m.YourCustomisable_NullableBool, "Customisable_NullableRadioBool")
    

    And the rendered output examples

提交回复
热议问题