3

I have a View for an Update operation, and I have a enum property of my Model:

public enum DeliveryStatusType
{
    Active = 1,
    Deactive = 2,
}

Basically, I populate the enum and using a DropDownList. To understand what is going on, I simplified it and posted here as simple as possible.

I've tried both Html.DropDownList and Html.DropDownListFor and here are the Views:

1st example (not setting the selected here):

@Html.DropDownListFor(model => model.DeliveryStatus, new SelectListItem[] {
        new SelectListItem { 
            Selected = false, 
            Text = "Active Delivery", 
            Value = "1" },
        new SelectListItem { 
            Selected = true, 
            Text = "Deactive Delivery", 
            Value = "2" }}) //Notice that I manually set the 2nd true

2nd example (not setting the selected here):

@Html.DropDownList("DeliveryStatus", new SelectListItem[] {
        new SelectListItem { 
            Selected = false, 
            Text = "Active Delivery", 
            Value = "1" },
        new SelectListItem { 
            Selected = true, 
            Text = "Deactive Delivery", 
            Value = "2" }}) //Notice that I manually set the 2nd true

But, what I realized is that when I set the name and the id of the SELECT html element other than DeliveryStatus (lets say DeliveryStatus1), it magically works!

3rd example (Setting the selected here):

@Html.DropDownList("DeliveryStatus1", new SelectListItem[] {
        new SelectListItem { 
            Selected = false, 
            Text = "Active Delivery", 
            Value = "1" },
        new SelectListItem { 
            Selected = true, 
            Text = "Deactive Delivery", 
            Value = "2" }}) //Notice that I manually set the 2nd true

But when I do that and set as DeliveryStatus1, I cannot post the data to the Controller.

What is wrong with this or what am I doing wrong? I need both to be able to SET THE SELECTED data and POST it back

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
E-A
  • 1,995
  • 6
  • 33
  • 47
  • Set the value of property `DeliveryStatus` to `DeliveryStatusType.Deactive` –  Oct 11 '14 at 11:18
  • possible duplicate of [DropDownList setting selected item in asp.net MVC](http://stackoverflow.com/questions/2410543/dropdownlist-setting-selected-item-in-asp-net-mvc) – Richard Szalay Oct 11 '14 at 11:19
  • I assume you wanted me to like "". But none of DropDownListFor or DropDownList worked with this – E-A Oct 11 '14 at 11:23
  • You are right Richard, but I did the same there, still not functioning properly. – E-A Oct 11 '14 at 11:33
  • 2
    No. The selected value of `@Html.DropDownListFor` is based on the value of the property, not the value of the `Selected` property of `SelectListItem`. Also I suggest you look at [this answer](http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) for creating an extension method –  Oct 11 '14 at 11:35

2 Answers2

2

Thanks to Stephen Muecke, for enums I gave the value as Enum.ToString() instead of the Integer value.:

@Html.DropDownListFor(model => model.DeliveryStatus, new SelectListItem[] {
        new SelectListItem { 
            Selected = false, 
            Text = "Active Delivery", 
            Value = "Active" },
        new SelectListItem { 
            Selected = true, 
            Text = "Deactive Delivery", 
            Value = "Deactive" }})
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
E-A
  • 1,995
  • 6
  • 33
  • 47
  • Note you don't need the set the `Selected` property in `SelectListItem`. If the value of `model.DeliveryStatus` is `DeliveryStatusType.Deactive` then the 2nd option will be selected hwne the view is displayed. –  Oct 11 '14 at 12:04
  • I am not manually setting in my actual project. Instead, I use `EnumDropDownListFor` which foreach within the enum class. But to simplify my problem I first wrote the code I posted above and start examining it. – E-A Oct 11 '14 at 12:49
0

what worked for me was to make sure the model.Xyz variable was a string and then using the "Selected = true" to make it the default selected value.

@Html.DropDownListFor(model => model.TopN, new SelectListItem[]
{
    new SelectListItem() { Text = "10", Value = "10", Selected = false },
    new SelectListItem() { Text = "20", Value = "20", Selected = false },
    new SelectListItem() { Text = "50", Value = "50", Selected = true },
    new SelectListItem() { Text = "100", Value = "100", Selected = false },
    new SelectListItem() { Text = "500", Value = "500", Selected = false }
})
Darrel K.
  • 1,611
  • 18
  • 28