C# Enum Values as Json ASP.NET Web API Response

I am working on a web app that is using AngularJS on the client side and in one of the forms I needed the user to be able to select time zones but I didn’t want to hard code it into the javascript, as well as it is getting stored on the back-end as my C# enum type USTimeZones:
public enum USTimeZones
{
AlaskanDaylightTime,
AlaskanStandardTime,
CentralDaylightTime,
CentralStandardTime,
EasternDaylightTime,
EasternStandardTime,
HawaiianStandardTime,
MountainDaylightTime,
MountainStandardTime,
MountainTime,
PacificDaylightTime,
PacificStandardTime,
USEasternStandardTime,
USMountainStanardTime
};
[HttpGet]
public IHttpActionResult GetTimeZonesJson()
{
var enumVals = new List<object>();
 
foreach (var item in Enum.GetValues(typeof(USTimeZones)))
{
enumVals.Add(new
{
id = (int)item,
name = item.ToString()
});
}
 
return Ok(enumVals);
}

Search This Blog

Arsip Blog

Powered by Blogger.

Recent

Comment

Author Info

Like This Theme

Popular Posts

Video Of Day

Sponsor

Most Popular