Kenny
Kenny

Reputation: 2186

Razor HTML dropdown list helper not selecting matching value

I have a view where I am creating a dropdown list. The list populates properly, but will not select the matching item for the page I'm editing. On this page I am editing a Sermon record. The sermon model has the id of the series that it belongs to. I have confirmed that the series_id in the sermon record, and the matching one in the dropdown list are both "1". I did this by debugging `model.sermon.Series_ID, and confirming that it was "1", and using Inspect in Chrome, I saw that there is a "1" on one of the items in the dropdown list.

Can anyone tell me why the dropdown list isn't selecting the matching series? There is no "selected" attribute being produced even though the number that matches is present.

View

@model SermonLibraryManager.ViewModels.ViewModel

@Html.DropDownListFor(model => model.sermon.Series_ID, new SelectList(Model.seriesList, "Series_ID", "Series_Title"), "-- Select --")

ViewModel

namespace SermonLibraryManager.ViewModels
{
    public class ViewModel
    {
        public SermonModel sermon;
        public List<SermonModel> sermonList;
        public SeriesModel series;
        public List<SeriesModel> seriesList;
    }
}

SermonModel

public class SermonModel
{
    public int Sermon_ID { get; set; }
    public string Series_Title { get; set; }
    public int Series_ID { get; set; }
    public string Sermon_Title { get; set; }
    public DateTime Sermon_Date { get; set; }
    public string Nickname { get; set; }
    public string Last_Name { get; set; }
    public string Topic { get; set; }
    public string Description { get; set; }
    public string UStream_ID { get; set; }
    public string Vimeo_ID { get; set; }
    public int? Main_Speaker { get; set; }
}

SeriesModel

public class SeriesModel
{
    public int Series_ID { get; set; }
    public string Series_Title { get; set; }
}

Upvotes: 0

Views: 629

Answers (1)

Karel Tamayo
Karel Tamayo

Reputation: 3760

After a very good discussion with @Stephen Muecke, I'm editing the answer in order to avoid other users to be confused.

The problem is that your view model is using a public field instead of a property and model binding doesn't work with fields, so in practice this is the same as using the Html.DropDownList() helper method and this method accepts any value as the selected value in the select list.

If you convert your fields into properties in your view model:

public class ViewModel
{
    public SermonModel sermon { get; set; }
    public List<SermonModel> sermonList { get; set; }
    public SeriesModel series { get; set; }
    public List<SeriesModel> seriesList { get; set; }
}

The the only thing you need to do is to call the Html.DropDownListFor() method and model binding will take care of everything for you:

@Html.DropDownListFor(model => model.sermon.Series_ID, new SelectList(Model.seriesList, "Series_ID", "Series_Title"), "-- Select --")

Another good advice is never use data models in your view models, so the best solution will be to have an IEnumerable<SelectListItem> in the viewmodel and generate the Drop Down in the view.

Thanks to @Stephen Muecke for pointing out the real issue.

Hope this helps!

Upvotes: 3

Related Questions