6
\$\begingroup\$

I'm currently working through Getting Started with EF5 using MVC 4 in Visual Studio 2010 (.NET 4.0) to learn about how to use ASP.NET and Entity Framework. During the beginning of the tutorial I ran into an issue with enum support when creating a database with Code First Migrations. For reasons unknown I have to work in this environment and create my own support for enums so I decided to make the property int instead then I would convert it to Enum when I want to display it like so:

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq;
using System.Linq.Expressions;

namespace ContosoUniversity.Extensions
{
    public static class Extensions
    {
        public static String DisplayAsEnum<TModel, TValue>(this HtmlHelper<TModel> cHtml,
                                                           Expression<Func<TModel, TValue>> cExpression,
                                                           Type cType,
                                                           String strDisplay = "Unknown")
        {
            try
            {
                if (cType.IsEnum)
                {
                    object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

                    if (cResult != null)
                    {
                        strDisplay = Enum.ToObject(cType, cResult).ToString();
                    }
                }
            }
            catch (Exception cExc)
            {
                strDisplay = cExc.ToString();
            }

            return (strDisplay);
        }
    }
}

In my razor cshtml file I display it to the webpage with this call:

@Html.DisplayAsEnum(modelItem => item.Grade, typeof(Grade), "No Grade")

I have a feeling that this is not the "professional" or "right" way of handling the lack of enum support so I was wondering if there is anyway to improve this function or is there a better way of handling enumerations in EF 5 with .Net 4.0 support?

I was shown a Stack Overflow question that provides me with a better solution to handling the enum support. It is very helpful and I am using it at the moment, but since I do like the function that I made, is it possible to improve it?

\$\endgroup\$
  • \$\begingroup\$ This StackOverflow question should be right up your alley. In my opinion the accepted answer is a cleaner solution. \$\endgroup\$ – Greg Burghardt Oct 6 '14 at 17:16
  • \$\begingroup\$ @GregBurghardt It looks like I should of paid more attention to StackOverflow before posting my question lol. That did work for me, and without needed to re-update the database. Thanks! \$\endgroup\$ – John Odom Oct 6 '14 at 18:43
5
\$\begingroup\$

Ideally you won't want to change the strDisplay, so we should probably take it out of the parameter list and make it a constant inside the method and then just return in each of the 3 circumstances.

var display = "Unknown";

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return display;

this seems weird to me that we are using the variable (whatever you choose to name it) only once, I would probably in this circumstance (since we aren't allowing it to be changed) just return the string Unknown like this

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return "Unknown";
\$\endgroup\$
  • 1
    \$\begingroup\$ I don't know why the other answer got deleted but I do like this answer. It does make sense to me to not change strDisplay. I don't know what I was thinking at the time lol. Thanks for the answer! \$\endgroup\$ – John Odom Apr 17 '15 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.