Invalid MVC DropDownList selection

I have a View to Update operation and I have a property on enum

my model:

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

      

Basically, I am filling in enum

and using DropDownList

. To understand what's going on, I've simplified it and put it as simple as possible here.

I tried both Html.DropDownList

and Html.DropDownListFor

, but Views:

1st example (not specified 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

      

Second example (not specified 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 I figured out that when I set the name and id of the SELECT

html element other than DeliveryStatus

(say DeliveryStatus1

), it magically works!

3rd example (setting 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 this and set as DeliveryStatus1

, I cannot send data to Controller

.

What is wrong with this or what am I doing wrong? I need both to be able to SET FAVORITE data and SEND it back.

+3


source to share


2 answers


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" }})

      

+2


source


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



@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 }
})

      

0


source







All Articles