asp.net-mvc - DropDownListFor, selected = true 不起作用

标签 asp.net-mvc selectlist dropdownlistfor

Select 不适用于我的 DropDownListFor。谁能帮我吗?

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

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

我的第一个模型:

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

我的第二个模型:

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)

最佳答案

DropDownListFor, selected = true doesn't work

是的。

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

当您使用时

// 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>

结果:

enter image description here

dotnetfiddle.net Example

关于asp.net-mvc - DropDownListFor, selected = true 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966553/

相关文章:

asp.net-mvc - 在 MVC 中绑定(bind)到 SelectList

c# - 如何将 IEnumerable<SelectListItem> 集合传递给 MVC 局部 View ,每个请求仅进行一次数据库查找

javascript - asp.net mvc 在 javascript 中打开另一个 View

asp.net-mvc - 存储库模式的接口(interface)有什么特殊用途?

javascript - 重置 SELECT 列表在 Firefox 中不起作用

asp.net-mvc - 如何在 SelectList 文本描述中组合两个字段?

c# - 将数据集数据加载到 DropDownList c#

c# - 如何在 C# 中使用 Openxml 在 Excel 中创建下拉列表

asp.net-mvc - 使用 asp.net mvc 用逗号分隔数字

jquery - Html.BeginForm 使用 onSubmit 进行验证