0

I have a dropdown list that is not showing the default value in the view. After debugging I can see that the default values are being pulled with the correct values but they are not being displayed. I store my IEnumerable in my ViewModel

Example: DropDown 1 is supposed to show True (that is the value in the DB) but is showing the first value "Not Set"

enter image description here

View

@model IList<NavLiveWebInterface.ViewModel.Proc_Item_SKUViewModel>

    for (var i = 0; i < Model.Count(); i++)
    {
            <div class="editor-label">
                @Html.LabelFor(m => Model[i].Filterable)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => Model[i].Filterable,Model[i].TrueFalse)
            </div>
                @Html.ValidationMessageFor(m => Model[i].Filterable,"Please Select a Value")
    }

View Model

public bool? Filterable { get; set; }
public IEnumerable<SelectListItem> TrueFalse
{
    get
    {
        return new[]
        {
            new SelectListItem { Value = "Not Set", Text = "Not Set" },
            new SelectListItem { Value = "True", Text = "True" },
            new SelectListItem { Value = "False", Text = "False" }
        };
    }
}
8
  • Where are you setting DropDownListFor to the value True? How would it know that true is the default?
    – Bit
    Jun 10, 2014 at 18:08
  • What type is Model[i].Filterable?
    – Steven V
    Jun 10, 2014 at 18:10
  • @Bit It's not really a "default" value its whatever is stored in the db so its either True or false I'll update my question I can see how it could be misleading
    – joetinger
    Jun 10, 2014 at 18:18
  • @StevenV bool? I'll it to my code
    – joetinger
    Jun 10, 2014 at 18:20
  • 1
    I wonder if you're running into this issue: stackoverflow.com/a/11045737/254973
    – Steven V
    Jun 10, 2014 at 18:24

1 Answer 1

2

DropDownListFor does not support matching a selected value from an indexed expression.

You need to create a SelectList for each entry, that has the current selected value passed to it.

This will work:

for (var i = 0; i < Model.Count(); i++)
{
    <div class="editor-label">
        @Html.LabelFor(m => Model[i].FilterableString)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => Model[i].Filterable, new SelectList(Model[i].TrueFalse, "Value", "Text", Model[i].Filterable));
    </div>
    @Html.ValidationMessageFor(m => Model[i].Filterable, "Please Select a Value")
}

You are also repeating the option of a drop-down list. It is only the SelectList created from the options that needs to be unique (in order to have a selected state) so you could share a single data list for your True, False and null options and provide it via the ViewBag (but don't forget to cast it back to an IEnumerable<SelectListItem> in the SelectList constructor or you get an error)

Update - Share the list:

You can create a single shared list in the ViewBag like this controller code:

ViewBag.Filterable = new List<KeyValuePair<bool?, string>>()
{
    new KeyValuePair<bool?, string>( null, "Not Set" ),
    new KeyValuePair<bool?, string>( true, "True" ),
    new KeyValuePair<bool?, string>( false, "False" )
};

I used a KeyValuePair as that is really what you are creating for your options.

and you access it in the view like this:

@Html.DropDownListFor(m => Model[i].Filterable, new SelectList((IEnumerable<KeyValuePair<bool?, string>>)ViewBag.Filterable, "Key", "Value", Model[i].Filterable));

One last word:

If you were not using indexed items, the simpler @Html.DropDown("Filterable") would automatically bind the value to the ViewModel (e.g. Model.Filterable) and the drop down list to a ViewBag property of the same name (e.g. ViewBag.Filterable). This can come in handy for shorter code in simpler situations.

2
  • Thanks for the response and the code it works great. I'm not 100% sure though on what you mean by "repeating the option of a drop-down list". Are you saying I should use a viewbag instead of having the drop down in my viewmodel?
    – joetinger
    Jun 11, 2014 at 12:29
  • Updated with practical example of a shared list. A drop down list never belongs in a single item's model (unless it were unique per item). Hope this does the trick for you :) Jun 11, 2014 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.