question

HadisSulejmani-3146 avatar image
0 Votes"
HadisSulejmani-3146 asked YijingSun-MSFT commented

Is there a method to show string values instead of boolean values with EditorFor() in ASP.NET MVC?

I have a form where user needs to select the gender in a dropdown list, and I am using EditorFor() for this purpose, which is working well, but is displaying values as True and False, instead of Male, Female and Gender is a boolean property. How to convert boolean values into strings in this case. The code is as below:

<dl>
<dt>@Html.LabelFor(model => model.EmployeeInfo.Person.Gender</dt>
<dd>
@Html.EditorFor(model => model.EmployeeInfo.Person.Gender)
@Html.ValidationMessageFor(model => model.EmployeeInfo.Person.Gender)
</dd>
</dl>

dotnet-aspnet-generaldotnet-aspnet-webformsdotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
1 Vote"
cooldadtx answered cooldadtx commented

If Gender is a boolean then what would you expect? If it isn't a boolean then that would be odd.

EditorFor just looks at the top of the property and selects a default control (textbox in most cases). In your case if you want a dropdown then use DropDownList instead. If Gender is an enum then you tell it what enum to render (which sets the label and value) along with the value to bind against. Under the hood the select item that has a value that matches the value of the property is marked as selected when rendering and set when posting.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

146207-image.png



At the moment the above solution is showing something as in the image. So it's working fine like this, dropdown just is more complicated and not for this case.

0 Votes 0 ·
image.png (10.7 KiB)
cooldadtx avatar image cooldadtx HadisSulejmani-3146 ·

But Gender is a boolean right? So what does it mean when Gender is true? Drop downs work based upon the value of the select item. If you are using a boolean you cannot change that. If you are using an enum then it will work properly.

If you cannot use an enum then you have no choice but to build the select manually and you cannot use EditorFor as it is not designed to convert boolean values to custom values. Of course you could also introduce a property on your model that exposes a Gender property that does use an enum and that would also work.

0 Votes 0 ·
cooldadtx avatar image cooldadtx HadisSulejmani-3146 ·

Post the type information for Gender please.

0 Votes 0 ·
cooldadtx avatar image cooldadtx HadisSulejmani-3146 ·

For a boolean Gender property something like this would work but it isn't ideal, an enum is far better.

@functions {
    public IEnumerable<SelectListItem> GetGenderList() {
        return new[] {
                new SelectListItem() { Text = "Male", Value = "true" },
                new SelectListItem() { Text = "Female", Value = "false" }
                };
    }
    }

@Html.DropDownListFor(model => model.Person.Gender, selectList: GetGenderList())


The select list is generated on the view side (since you said you cannot touch the server even though you actually are). It then maps the values of Gender to the corresponding select items.

0 Votes 0 ·
YijingSun-MSFT avatar image
1 Vote"
YijingSun-MSFT answered YijingSun-MSFT commented

Hi @HadisSulejmani-3146 ,
Why you don't use @Html.DropDownListFor? It's easy for drop downs.
Just like this:

 @Html.DropDownListFor(m => m.StudentGender, 
             new SelectList(Enum.GetValues(typeof(Gender))), 
             "Select Gender")

 public class Student
 {
     public int StudentId { get; set; }
     public string StudentName { get; set; }
     public Gender StudentGender { get; set; }
 }
    
 public enum Gender
 {
     Male,
     Female    
 }

Do you just only want to use @Html.EditorFor?

Best regards,
Yijing Sun


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I cannot change the logic part of the code, I need to change only the view .cshtml file, with whatever it is. Simpler better.

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe HadisSulejmani-3146 ·

But, the bug is located in the unconventional logic design. Gender is male/female not true/false. How will the user know what to select? Makes no sense...

0 Votes 0 ·

Hi @HadisSulejmani-3146 ,
Could you post your logic codes about your model and controller?
Best regards,
Yijing Sun

0 Votes 0 ·