0

Hi am kind of new to MVC,

The .tt file corresponding to the table in the DB, has a data type as String for Gender. I have manually declared an enum for Gender in my C# class. I have also implemented the Partial class concept and used metadata to change the datatype of the property.

But that does not seem to have any influence on the Model when I am accessing it in the Controller. I still get the datatype of the property Gender as string and not as Enum.

I have created separate Enums for all the dropdowns across the application and now i am facing this problem.

please help.

Vicky-Torres
  • 219
  • 2
  • 8

1 Answers1

0

I would suggest that you don't change the data type of the class that's generated by the designer. If it's some type of char in the db then the type in the designer is correct per the db schema. One solution is to define the enums and decorate them with a description attribute using an extension method like below:

public static string ToDescription(this Enum value)
    {
        var da =
            (DescriptionAttribute[])
                (value.GetType().GetField(value.ToString())).GetCustomAttributes(typeof(DescriptionAttribute),
                    false);
        return da.Length > 0 ? da[0].Description : value.ToString();
    }

Then decortate your enum like this:

public enum SomeType
    {
        [Description("Type1")] FirstType,
        [Description("Type2")] SecondType,
        [Description("Type3")] ThirdType
    }

That will allow you to call SomeType.FirstType.ToDescription() to pass the value to your entity property.

El Kabong
  • 717
  • 1
  • 8
  • 15
  • Hi ... I am using the same attribute as you had mentioned. I am using the custom HtmlHelper to populate the Dropdown for gender from Enum. I also have a StringValue attribute class which has the description as you have mentioned. I am not able to pass the Enumeration name to the HtmlHelper from the view so that i can populate the dropdown. – Vicky-Torres Jun 29 '14 at 18:25
  • Create a List myEnumsDropDownValues property on your view model to store the dropdown values and use that in your view. – El Kabong Jun 30 '14 at 13:15