DropDownListFor, selected = true doesn't work

2020-03-30 06:23发布

问题:

Select doesn't work for me with DropDownListFor. Can anyone help me?

I have musiccategories and artists that belong to one musiccategory. On my page I want to show artist details, and I want the dropdownlist to load all musiccategories with the specified artists music category selected. But I can't make one specified option in the drop down list selected, the first option is always selected at first.

My controller:

public ActionResult Index()
{
      ClassLibrary.Artist a = GetArtist();
      System.Collections.Generic.List<System.Web.Mvc.SelectListItem> items = getGenres();
      string genre = a.MusicCategory;
      foreach (SelectListItem sli in items)
      {
          if (sli.Text == genre)
          {
              sli.Selected = true;
          }
      }
      ViewBag.MusicCategory = items;
      return View(a);
}

My first model:

public class MusicCategory
{
    public int MusicCategoryID { get; set; }
    public string MusicCategoryName { get; set; }
}

My secound model:

public class Artist
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public string MusicCategory { get; set; }
        public int MusicCategoryID { get; set; }
        public int Contact { get; set; }
        public string InformationToCrew { get; set; }
        public string Agreement { get; set; }
        public string WantedStage { get; set; }
        public string AgreementAccepted { get; set; }
        public string PublishingStatus { get; set; }
        public string ApplicationStatus { get; set; }
        public int? ActiveFestival { get; set; }
        public string ImageURL { get; set; }
        public string URL { get; set; }
        public string FacebookEvent { get; set; }
        public int Score { get; set; }
        public List<GroupMember> GroupMembers { get; set; }
    }

My view:

@Html.DropDownListFor(model => model.MusicCategory, (System.Collections.Generic.List<System.Web.Mvc.SelectListItem>)ViewBag.MusicCategory)

回答1:

DropDownListFor, selected = true doesn't work

Yup.

But I can't make one specified option in the drop down list selected, the first option is always selected at first.

When you use

// I don't recommend using the variable `model` for the lambda
Html.DropDownListFor(m => m.<MyId>, <IEnumerable<SelectListItem>> ...

MVC Ignores .selected and instead verifies the m.<MyId> value against the values in <IEnumerable<SelectListItem>>.

public class DropDownModel
{
    public int ID3 {  get; set; }
    public int ID4 { get; set; }
    public int ID5 { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

public ActionResult Index()
{
    var model = new DropDownModel
    {
        ID3 = 3,  // Third
        ID4 = 4,  // Second
        ID5 = 5,  // There is no "5" so defaults to "First"
        Items = new List<SelectListItem>
        {
            new SelectListItem { Text = "First (Default)", Value = "1" },
            new SelectListItem { Text = "Second (Selected)", Value = "2", Selected = true },
            new SelectListItem { Text = "Third", Value = "3" },
            new SelectListItem { Text = "Forth", Value = "4" },
        }
    };
    return View(model);
}

<div>@Html.DropDownListFor(m => m.ID3, Model.Items)</div>
<div>@Html.DropDownListFor(m => m.ID4, Model.Items)</div>
<div>@Html.DropDownListFor(m => m.ID5, Model.Items)</div>

Result:

dotnetfiddle.net Example



回答2:

Maybe it has something to do with the way you populate your selec list items or your model.

You can take a look at this post :

How can I reuse a DropDownList in several views with .NET MVC