1

I'm trying to set the selected Value for a drop down list.

In Controller I have

ViewData["productType"] = new SelectList(myDropDownList);

And in the View:

<%= Html.DropDownList("productType", ViewData["productType"] as SelectList, "defaultValue")%>

So far So good, but the problem is that now I have "defaultValue" twice in my drop down list. so I have all my values in dropdownlist including the "defaultValue". but this code will add the "defaultValue" as the first element. and I see two samples of it.

I like to set the selected valiue to "defaultValue" without adding it twice.

I tried

ViewData["productType"] = new SelectList(myDropDownList, "defaultValue" );

but it didn't work.

Can anyone please tell me what to do?

1 Answer 1

5

You should not be using the same name as first argument for the dropdown as the second one. In your example you have used productType for storing both the selected value and the list of available values. In order to render a DropDown in ASP.NET MVC you need 2 properties:

<%= Html.DropDownList(
    "selectedProductType", 
    ViewData["productType"] as SelectList, 
    "defaultValue"
) %>

and inside your controller action you could set those 2 properties:

ViewData["selectedProductType"] = "abc";
ViewData["productType"] = new SelectList(myDropDownList);

this assumes that you already have an element with value="abc" inside your dropdown list of product types. The value will then be automatically preselected.

I would recommend you an alternative approach though for rendering dropdown lists. It consists of getting rid of view data and introducing view models and using the strongly typed version of the helper:

public class ProductViewModel
{
    public string SelectedProductType { get; set; }
    public IEnumerable<SelectListItem> ProductTypes { get; set; }
}

then you will have a controller action that will populate this view model and pass it to the view:

public ActionResult SomeAction()
{
    var model = new ProductViewModel();

    // preselected an element with value = "type2"
    model.SelectedProductType = "type2";

    // bind the available product types
    model.ProductTypes = new SelectList(
        // Obviously those could come from a database or something
        new[] {
            new { Value = "type1", Text = "product type 1" },
            new { Value = "type2", Text = "product type 2" },
            new { Value = "type3", Text = "product type 3" },
            new { Value = "type4", Text = "product type 4" },
        }, 
        "Value", 
        "Text"
    );

    // pass the view model to the view
    return View(model);
}

and finally inside your strongly typed view:

<%= Html.DropDownListFor(
    x => x.SelectedProductType, 
    Model.ProductTypes, 
    "defaultValue"
) %>
Sign up to request clarification or add additional context in comments.

1 Comment

I swear, if I had a nickel for every post where someone uses the same name for both, i'd be quite rich. I have no idea why this is such a common thing. It seems like several times a day this same problem shows up.

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.