2

I am working with a list of string items in mvc that needs to be selected from a drop down list. The drop down list is binding fine, and value's are setting fine, but even though the current item being iterated matches an item in the drop down list it isn't being pre-selected as it's value, can anyone point me in the right direction?

    @for (var i = 0; i < Model.StringList.Count; i++)
    {
        if (BL.Helpers.StringHelpers.Validate(Model.DisplayStringSegments[i]))
        {
            <div id="editor-field">
                @Html.DropDownListFor(m => m.StringList[i], Model.PosterOptions, String.Empty, new { })
            </div>
        }
        else
        {
            <div id="editor-label">@Model.StringList[i]</div>
            @Html.HiddenFor(model => model.StringList[i])
        }
    }

So for this case, the Options is a list of strings holding only one value, "Test" -> set both as Text and Value;

       PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test" });

Can anyone tell me why the current StringList[i] isn't being pre selected, even though it has the value of "Test" ?

1
  • You're absolutely certain StringList[0]'s value is "Test" (it's case sensitive)? Commented Oct 7, 2013 at 16:16

3 Answers 3

4

For anyone that comes across this;

I had to "Hack" a solution, I did this by:

Changing my ViewModel's (Model.Options)

List<SelectListItem> to a List<string>

Changing my drop down list selection to the following, forcing the selected value;

            <div id="editor-field">
                @{
                    string currentString = Model.StringList.ElementAt(i).ToString();
                 }

                @Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, currentString), String.Empty, new {})
            </div>

Perhaps there is a better way, but this works!

Sign up to request clarification or add additional context in comments.

Comments

0

Another way could be setting the current selected item during the list creation, like this:

PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test", Selected = true });

3 Comments

Fals, thanks for getting back to me. As these values are set per item in list, but each item in the list uses the same Drop Down, this wouldn't work. This is because I need the value held in each element to be selected, not an overall default.
You can set the Selected property based on any conditional in your controller. This is not necessaraly setting the default. You can retrieve a value from somewhere, compare to the current SelectListItem and then set it as selected or not.
As this is working with a List of string, I end up with n items using the same drop down, with y options within the drop down list. Each of the n items will require a different selected value based on it's value, whereas the y options remain the same for each item. For example, if I have 6 items within PosterOptions and 72 items within StringList, I will have 72 drop down lists rendered, each with a varying PosterOptions item selected. I don't wish to create multiple lists for the Drop Downs
0

I had the same issue and your response helped me. I don't think that's a "hack" though. Because in your question you were using the same SelectList for all the dropdownlists so even though you mention you didn't want to create multiple lists for the drop downs I can't see another way when you have multiple drop downs as you need to specify different selected values.

As a small refactoring you can get rid of the temp variable and access the selected value directly like this:

@Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, Model.StringList[i]), String.Empty, new {})

In your example you don't need to distinguish between text and value but in my case it was required. When that's necessary it can be accomplished by providing the value and text field names for the SelectList. For example, say you need multiple dropdowns with Country values such as:

Country class:

public class Country
{
    public string Code { get; set; }
    public string Name { get; set; }
}

Model:

public List<string> CustomerCountryList { get; set; }
public IEnumerable<Country> CountryList { get; set; }

and the View:

@Html.DropDownListFor(m => m.CustomerCountryList[i], new SelectList(Model.CountryList, "Code", "Name", Model.CustomerCountryList[i]))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.