DropDownListFor,selected = true不起作用

时间:2014-04-09 15:01:17

标签: asp.net-mvc selectlist dropdownlistfor

使用DropDownListFor选择不适用于我。任何人都可以帮助我吗?

我有属于一个音乐类别的音乐类别和艺术家。在我的页面上,我想显示艺术家的详细信息,我希望下拉列表加载所有选定指定艺术家音乐类别的音乐类别。但是我无法在所选的下拉列表中创建一个指定选项,首先始终选择第一个选项。

我的控制器:

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);
}

我的第一个模特:

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; }
    }

我的观点:

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

2 个答案:

答案 0 :(得分:2)

  

DropDownListFor,selected = true不起作用

烨。

  

但我无法在所选的下拉列表中创建一个指定选项,首先始终选择第一个选项。

使用时

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

MVC忽略.selected,而是根据m.<MyId>中的值验证<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>

结果:

documentation

enter image description here

答案 1 :(得分:-1)

可能与您填充选择列表项或模型的方式有关。

你可以看一下这篇文章:

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