Let’s say that you have class Person
and one of its properties is enum (in our case it’s enum Gender
).
public enum Gender { Unknown, Male, Female }
Below you can see Razor Page with a simple HTML form. Please remember to add the using statement to the folder where is defined your enum type (in my case it’s @using SelectBindingEnum.Models
).
<div class="form-group"> <label asp-for="Person.Gender" class="control-label"></label> <select asp-for="Person.Gender" asp-items="Html.GetEnumSelectList<Gender>()" class="form-control"></select> <span asp-validation-for="Person.Gender" class="text-danger"></span> </div>
As you can see above to binding enum values to HTML select tag you should use Tag Helper asp-items
and HtmlHelper method GetEnumSelectList<T>
where T
is an enum type. This method generates a SelectList object for all enum values. Also, this method allows you to add Display
attribute to your enum to change display text.
public enum Gender { [Display(Name = "None")] Unknown, Male, Female }
If you would add this attribute, you will see the following values in HTML select tag: None, Male and Female.
Some properties of model classes come as enums and we want to show Enum values in a select list. Sometimes we want enum element names but sometimes we want to use custom names or even translations.
Let’s define the enum and make it use DisplayAttribute and resource file.
public enum CustomerTypeEnum { [Display(Name = "Companies")] PrivateSector, [Display(Name = "PublicSector", ResourceType = typeof(Resources.Common))] PublicSector, Internal }
There are three different cases together now:
- Enum member with just a name
- Enum member with Display attribute and static name
- Enum member with Display attribute and resource file
Important thing: set resource modifier to Public.
Now use the Html.GetEnumSelectList()
extension method to fill the select list with enum members. Notice the first empty selection (Select type …) as the only member of the select list.
<div class="form-group"> <label asp-for="Type" class="control-label"></label> <select asp-for="Type" class="form-control" asp-items="Html.GetEnumSelectList<CustomerTypeEnum>()"> option>Select type ...option> </select> <span asp-validation-for="Type" class="text-danger"><span> <div>
When we run the application we can see that the select list is filled with enum members and ASP.NET Core respects DisplayAttribute with static name and resource files.
If you don’t have control over enum (enum is defined in already built assembly) then you can’t apply Display attribute to enum and you need some other solution to get values you need.
Wrapping up
ASP.NET Core has built-in Html.GetEnumSelectList()
method we can use to turn enum members to select list items. The method is intelligent enough to support the Display attribute and therefore we can use custom names for enum members and even translations from resource files.
Here’s my claims transformation that adds roles to user identity.
public class AddRolesClaimsTransformation : IClaimsTransformation
{
private readonly IUserService _userService;
public AddRolesClaimsTransformation(IUserService userService)
{
_userService = userService;
}
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
// Clone current identity
var clone = principal.Clone();
var newIdentity = (ClaimsIdentity)clone.Identity;
// Support AD and local accounts
var nameId = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier ||
c.Type == ClaimTypes.Name);
if (nameId == null)
{
return principal;
}
// Get user from database
var user = await _userService.GetByUserName(nameId.Value);
if (user == null)
{
return principal;
}
// Add role claims to cloned identity
foreach(var role in user.Roles)
{
var claim = new Claim(newIdentity.RoleClaimType, role.Name);
newIdentity.AddClaim(claim);
}
return clone;
}
}
The following sample will return JSON with a 200 OK status code (it's an Ok type of ObjectResult)
public IActionResult GetAsJson() { return new OkObjectResult(new Catagory { Id = 1234, Name = "Articles" }); }
The following code uses the Produces() attribute (which is a ResultFilter) with contentType = application/json
[Produces("application/json")] public Student GetWithProduces() { return new Catagory { Id = 1234, Name = "Articles" }; }
Return JSON with a 400 error code with a message
public IActionResult ErrorJSON() { return new JsonResult(new { message = "The Error Messages" }) { StatusCode = StatusCodes.Status400BadRequest // Status code here }; }
How to return JSON in camelCase?
Configure in Startup.cs
First of all, add this dependency:
using System.Text.Json;
Then add this in ConfigureServices():
services.AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
Change JsonSerializer options
Supply JsonSerializer.Serialize() with the optional JsonSerializerOptions object and override the casing:
string jsonString = JsonSerializer.Serialize(m, new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
Comments