2

I am new to MVC. I am using the following enum in my Orders table:

public enum OrderStatus
    {
        Pending = 1,
        Held = 2,
        [Display(Name = "In Process")]
        In_Process = 3,
        Completed = 4,
        Shipped = 5,
        Returned = 6,
        Cancelled = 7
    }

and in the model:

public class Order
    {
        ...
        public OrderStatus OrderStatus { get; set; }
    {

in the view:

                <div class="form-group">
                    @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EnumDropDownListFor(model => model.OrderStatus,
                              "--Select--", 
                              new { @class = "form-control ingUOM" })  
                            @Html.ValidationMessageFor(model => model.OrderStatus, "", new { @class = "text-danger" })
                     </div>
                 </div>

in the controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(OrderViewModel ovm, int id)
...

 Order order = db.Orders.FirstOrDefault(o => ((o.OrderId == id)));

                order.OrderStatus = ovm.OrderStatus;

My issue is that I want to save the string value of the enum, instead of its integer value. This is so it will be easier to display the string value of the enum in reports and other views. I have been researching this but have been unsuccessful in finding a technique to use. Any help will be much appreciated.

9
  • Have you tried the ToString() method on your enum? Commented May 10, 2017 at 20:44
  • Can you give an explicit example of this? Commented May 10, 2017 at 20:45
  • e.g. OrderStatus.ToString() (the variable, not the enum type) Commented May 10, 2017 at 20:47
  • I tried: order.OrderStatus = ovm.OrderStatus.ToString() in the controller, but intellisense doesn't like it. Commented May 10, 2017 at 20:54
  • Is Order a model in Entity Framework? If so, and you are code first, you just need to change the signature to string or add a new property of string. Intellisense wont like you assigning a string to OrderStatus Commented May 10, 2017 at 20:55

1 Answer 1

1

You could use a helper class to try to get the value of the Display attribute if it exists for the enum member and fallback to call ToString() if it's not found. Something like this should do the trick:

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

...

public static class DisplayAttributeHelper
{
    public static string ReadDisplay(Enum target)
    {
        var attrs = target.GetType().GetMember(target.ToString())
            .First()
            .GetCustomAttributes(typeof(DisplayAttribute), false)
            .Cast<DisplayAttribute>();

        foreach (var attr in attrs)
            return attr.GetName();

        return target.ToString();
    }
}

Now you can keep storing your enum value normally using EF or linq and when you need to show the string value call the helper class.

For instance to show that in a report you could create your report model:

public class ReportModel
{
        public OrderStatus OrderStatus { get; set; }

        public string OrderStatusDisplayText => DisplayAttributeHelper.ReadDisplay(OrderStatus);
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.