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.
ToString()method on your enum?OrderStatus.ToString()(the variable, not the enum type)Ordera model in Entity Framework? If so, and you are code first, you just need to change the signature tostringor add a new property ofstring. Intellisense wont like you assigning astringtoOrderStatus