3

I have to set the selected item for a dropdownlist in view. But its not working.

//View

<div class="editor-label">
            @Html.LabelFor(model => model.Gender)
        </div>
        <div class="editor-field">
           @Html.DropDownListFor(model => model.Gender,Model.GenderList)
        </div>

//Model

[Required(ErrorMessage = "Please select gender!")]
        [Display(Name = "Gender")]
        public string Gender { get; set; }

  public IEnumerable<SelectListItem> GenderList
        {
            get
            {
                return new[]
            {
                new SelectListItem { Value = "0", Text = "Select" },
                new SelectListItem { Value = "1", Text = "Male" },
                new SelectListItem { Value = "2", Text = "Female" },
            };
            }
        }

The gender property has the value needs to be selected in the list. Still not working.

Where i'm wrong?

A Coder
  • 3,039
  • 7
  • 58
  • 129
  • Show us your Get action that sets the value for Model.Gender – Erik Funkenbusch May 10 '14 at 20:15
  • 1
    Are you putting "1" or "Male" in Gender? It should be the number. Also, you shouldn't make Select an item in the list. You should instead use the parameter of DropDownListFor that allows you to set the Unselected text. – Erik Funkenbusch May 10 '14 at 20:35
  • @ErikFunkenbusch: Yes i was trying to assign the text. Now its working fine. Thanks. – A Coder May 11 '14 at 05:36

2 Answers2

5

First, create your ViewModel.

public class MovieViewModel
{
    public string Genre { get; set; }

    public IEnumerable<SelectListItem> GenreList
    {
        get
        {
            yield return new SelectListItem { Text = "Comedy", Value = "1" };
            yield return new SelectListItem { Text = "Drama", Value = "2" };
            yield return new SelectListItem { Text = "Documentary", Value = "3" };
        }
    }
}

Then, your controller creates a new instance of this ViewModel and sends it to the view.

public class HomeController : Controller
{
    //
    // GET: /Home/
    public ActionResult Index()
    {
        var viewModel = new MovieViewModel
        {
            Genre = "2"
        };

        return View(viewModel);
    }
}

Finally, the view displays the dropdownlist using the ASP.NET wrapper [Html.DropDownListFor()][1].

@model MvcApplication1.Models.MovieViewModel

<!DOCTYPE html>

<html>
<head>
    <title>My movie</title>
</head>
<body>
    <div>
        @Html.DropDownListFor(m => m.Genre, Model.GenreList)
    </div>
</body>
</html>

The selected value is then automatically chosen according the ViewModel. There's no need to manually set the Selected property of the objects SelectListitem.

Jämes
  • 6,945
  • 4
  • 40
  • 56
-2

The SelectListItem type has a Selected property, you need to is to true on the item you wish to set as selected.

You can either set it statically like the following:

public IEnumerable<SelectListItem> GenderList
    {
        get
        {
            [...]
            new SelectListItem { Value = "0", Text = "Select", Selected = true},
            [...]
        }
    }

Or enumerate over the collection for a match with Gender value:

var selected = GenderList.SingleOrDefault(item => item.Text.Equals(this.Gender));
if (selected != null)
    selected.Selected = true;
Matan Shahar
  • 3,190
  • 2
  • 20
  • 45