简体   繁体   中英

How to convert the ENUM value to String from view to a controller

So I already know how to get the string,int,decimal and now I'm struggling in this part of enum. How can I convert my enum into a string

**Enum statecode = apsp.Customer.BillingAddress.StateCode.Value;**
Customer = new Customer()
       {
         FirstName = fn,
         LastName = ln,
         BillingAddress = new Address
             {
              StreetAddress1 = street1,
              StreetAddress2 = street2,
              City = city,
              **StateCode = statecode**,
              ZipCode = zipcode
              }
        },

so I want to insert the value of this..

Enum statecode = apsp.Customer.BillingAddress.StateCode.Value;

to this enum

StateCode = statecode,
Enum statecode = apsp.Customer.BillingAddress.StateCode;
Customer = new Customer()
   {
     FirstName = fn,
     LastName = ln,
     BillingAddress = new Address
         {
          StreetAddress1 = street1,
          StreetAddress2 = street2,
          City = city,
          StateCode = (StateCode)statecode,
          ZipCode = zipcode
          }
    },

If .ToString() doesn't work, you can also write your own parser, something like:

string EnumToString(Enum myEnum)
{
   switch (myEnum)
   {
      case MyEnumSpecialType :
         return "MyEnumSpecialType";
       //....
      default : 
         return "";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM