Question

I have added a DropDownListFor to my view.

@Html.DropDownListFor(m => m.SelectedItem_ID,Model.ItemsRepository)

The list is rendering fine with all the SelectItem objects in my list. However in my SelectItemList there is one SelectItem object that has been set to selected = true. I can place a debug point on the line and I see the property set to true but when the html renders the selected item is never default. It appears to be a bug but I'm not sure. I have tried adding the item manually in a string with another comma no dice. Anyone experiencing the same thing. Thanks in advance.

Was it helpful?

Solution 2

I was not clearing my model state before returning my view. I added the following in my controller before I cleared my view and it fixed my issues.

ModelState.Clear();

OTHER TIPS

I add here some code I used in one of my projects.

private SelectList GetProjectItemType()
        {
            var values = new List<SelectListItem>();
            values.Add(new SelectListItem() { Value = "Project", Text = "Project" });
            values.Add(new SelectListItem() { Value = "Bug", Text = "Bug" });
            values.Add(new SelectListItem() { Value = "Feature", Text = "Feature" });
            values.Add(new SelectListItem() { Value = "Improvement", Text = "Improvement" });
            return new SelectList(values, "Value", "Text", values[1]);
        }

The last parameter from SelectList constructor is going to specify which value is going to be selected as the default one.

Hope this is going to help you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top