I am using blazor webclient to display an enum value in the UI. however when I run the client and a value is assigned nothing displays. I have taken this code from the GetName method. however this displays nothing on the page.
my razor component
<div>
<p>Nationality: @Enum.GetName(typeof(Nationality), user.Nationality).ToString()</p>
<p>Favourite Medium: @Enum.GetName(typeof(Medium), user.FavouriteMedium).ToString()</p>
</div>
@code {
[Parameter]
public UserViewModel user {get; set;}
}
when the page is loaded in no error is given and the value is shown as blank. I followed this method from the documentation here.
https://docs.microsoft.com/en-us/dotnet/api/system.enum.getname?view=net-6.0
Edit.
The issue here was that there was a view model conversion point on my API that was not sending the enum value to the user model. because of this a 0 was being passed by the value where the enum values for these objects start at 1. therefore nothing was being displayed by the component where the @code block was inserted into the HTML.
thanks guys!
CodePudding user response:
Not sure what your code is doing, but here's a dirty demo that shows you how to display an enum in a Blazor component.
@page "/"
<h1>Hello, your @nationality</h1>
<div>
<button @onclick=ChangeNationality>Change Nationality</button>
</div>
@code {
private Nationality nationality = Nationality.French;
private void ChangeNationality()
=> nationality ;
public enum Nationality
{
French,
English,
Welsh,
Portuguese,
Spanish
}
}
CodePudding user response:
I found the issue to be that the enum was getting a value of 0 from the model object when the first value of enum was set to 1. therefore no value was being returned from the GetName() method.