V
V
Vugar Panahov2018-02-04 16:15:16
ASP.NET
Vugar Panahov, 2018-02-04 16:15:16

C# how to use an attribute to change the output of a property of type bool?

Good evening.
Can you tell me how to change the bool type output in html markup using an attribute in C#?
Class code:

public class Student
{
        [DisplayName("Пол")]
        public bool? Gender { get; set; }
        [DataType(DataType.Date)]
        [DisplayName("Дата рождения")]
        public DateTime? Birthday { get; set; }
}

Code in View:
@model NameSpace.Models.DataModel.Student

@Html.EditorForModel(new { htmlAttributes = new { @class = "form-control" } })

With this code, the following result is obtained:
5a77062e3edb1470356381.jpeg
I would like to know how you can change the code so that instead of false / true it shows, for example, male / female

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vugar Panahov, 2018-02-04
@Vug

Found the following solution:
Add the following attribute to the class [UIHint("Gender")]

public class Student
{
        [DisplayName("Пол")]
        [UIHint("Gender")]
        public bool? Gender { get; set; }
        [DataType(DataType.Date)]
        [DisplayName("Дата рождения")]
        public DateTime? Birthday { get; set; }
}

Now let's redefine the display and editing templates
Link
In the Views folder in the Shared folder, create a new folder EditorTemplates.
Add a new View to this folder: Gender.cshtml
@model bool?
    
<select class="list-box tri-state" id="Gender" name="Gender"><option value="">Не задано</option>
    <option selected="selected" value="true">Мужской</option>
    <option value="false">Женский</option>
</select>

5a771f7a0286b572674673.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question