0

I have a enum for a field status as

  public status? Status { get; set; }

and the class as

 public enum status
    {
        Incomplete = 0,
        InReview,
        Live,
        Rejected,
        Suspended,
        Scheduled,
        Delisted
    }

And in my model i populate my view using

var listStatus = new List<SelectListItem>();
listStatus.Add(new SelectListItem { Selected = true, Text = "Incomplete", Value = "Incomplete" });
listStatus.Add(new SelectListItem { Selected = false, Text = "Ready for Review", Value = "InReview" });

            model.Status = listStatus;
     //above are in controller to initialise z model's list
    [Display(Name = "Status")]
    public IEnumerable<SelectListItem> Status { get; set; }
    public status selectedStatus { get; set; }

In view

 @Html.DropDownListFor(m => m.selectedStatus, Model.Status)

ISSUE ?

On submit of the form I want to verify the choice of the user to as to proceed I mean I want to do something as

if(selectedStatus = "Incomplete")
{
//my codes hre
}

Question:

How do I access the Enum value matching the user's choice in the controller ?

Nishant Rambhojun
  • 77
  • 1
  • 2
  • 10
  • 1
    http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc –  Apr 04 '14 at 06:25

2 Answers2

0

Just posting the solution that worked for me for future visitors.

            var  chosenStatus = model.selectedStatus;

            if (chosenStatus == status.Incomplete)
            {

            }
Nishant Rambhojun
  • 77
  • 1
  • 2
  • 10
0

Try this.

In Get action:

var statuses = from Status s in Enum.GetValues(typeof(Status)) 
               select new { Id = (int)s, Name = s.ToString() };

ViewBag.Statuses = new SelectList(statuses, "Id", "Name", (int)Status.Live /*selected*/ );

In View:

@Html.DropDownListFor(model => model.StatusId, ViewBag.Statuses as IEnumerable<SelectListItem>, "Select a status" )

And in Post action:

if(model.StatusId == (int)Status.InReview)
{
   // do your work..
}
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90