how use enum that is returning string in C#

Viewed 52

I try to create a Rest API C# endpoint so they can post the JSON to that and it will process it. This is sample JSON they will post to my endpoint is looks like this:

 {
   "Guid": "abc123",
   "ID": "68AA101C-111-888-9CC1-1265",
   "Name": "test",  
   "formData": [
   {
   "FieldGuid": "454545454545",
   "FieldType": "a",
   "Label": "First name",
   "Value": "Sam"
    },
   {
  "FieldGuid": "121212121254545",
  "FieldType": "a",
  "Label": "Last name",
  "Value": "DummyData"
   },
   {
  "FieldGuid": "787878787854545",
  "FieldType": "b",
  "Label": "Date of Birth",
 "Value": "1999-01-01T16:05:00.000Z"
 },
 {
"FieldGuid": "2323212121545",
"FieldType": "c",
"Label": "Gender",
"Value": "Male"
},

......
 ] }

As you see "formData" is a lot of properties (it is like 40) . what is the best way to add "formData" to my model?

This is my model so far:

  public class Data
   {
   }

   public class FormData 
   {
    public string FieldGuid { get; set; }
    public string FieldType { get; set; }
    public string Label { get; set; }
    public object Value { get; set; }
   }

  public class Root
   {
   public string Guid { get; set; }
   public string ID { get; set; }
   public string Name { get; set; }
   public Data Data { get; set; }
   public List<FormData > formData { get; set; }
  }

and this is my API endpoint:

[HttpPost("CreateOT")]
public OPIEPatientDto CreateOT([FromBody] OTDto dto)
{
    SaveDto saveDto = new SaveDto();
    foreach (var item in dto.FormData)
    {
        switch (item.Label)
        {
            case "First name":
                saveDto.genericData.FirstName = item.Value.ToString();
                break;

            case "Last name":
                savePatientCommandDto.genericData.LastName = item.Value.ToString();
                break;

            case "Date of Birth":
                savePatientCommandDto.genericData.DateOfBirth = Convert.ToDateTime(item.Value);
                break;
        }

    }
...
}

Even this model and using foreach loop okay here? and also if instead of case "First name": I wanted to use enum what can I so?

I created an enum like:

public enum FormDataEnum
{
    Firstname = 0,
    Lastname = 1,
    DateofBirth = 2,
    Gender = 3,

.....

but when I wanted to use like this:

  switch (item.Label)
              {
                case FormDataEnum.Firstname.ToString():

does not recognize Firstname.I know we don't have enum as string in C#, I can use struct or const, what how I can use enum?

1 Answers

First make Label property of type FormDataEnum inside the class.

Then you can easily perform:

switch(varLabel)//the enum variable
{
   case FormDataEnum.FirstName:
   {
   //code
   break;
   }
  case FormDataEnum.Lastname:
  {
  //code
  break;
  }
 default:
 {
 //something
 }
}

Update:

 public class FormData
    {
        public enum FormDataEnum
        {
            Firstname = 0,
            Lastname = 1,
            DateofBirth = 2,
            Gender = 3
        }

        public FormDataEnum Label {get;set;}    

        public FormData(){}    
    }    

class Program
    {
        static void Main(string[] args)
        {
            List<FormData> list = new List<FormData>();

            FormData f1 = new FormData();
            f1.Label = FormData.FormDataEnum.Firstname;
            list.Add(f1);

            FormData f2 = new FormData();
            f2.Label = FormData.FormDataEnum.DateofBirth;
            list.Add(f2);

            FormData f3 = new FormData();
            f3.Label = FormData.FormDataEnum.Gender;
            list.Add(f3);    

            foreach (var formData in list)
            {    
                switch (formData.Label)
                {
                    case FormData.FormDataEnum.Firstname:
                        {
                            Console.WriteLine("FirstName");
                            break;
                        }
                    case FormData.FormDataEnum.DateofBirth:
                        {
                            Console.WriteLine("DateofBirth");
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("default");
                            break;
                        }

                }

            }
            Console.ReadLine();

        }
    }
Related