2014-04-09 81 views
1

Select對DropDownListFor不起作用。誰能幫我?DropDownListFor,selected = true不起作用

我有屬於一個音樂類別的音樂類別和藝術家。在我的頁面上,我想要顯示藝術家的詳細信息,並且我希望下拉列表能夠加載所選音樂類別的所有音樂類別。但是我不能在下拉列表中選擇一個指定的選項,首先選擇第一個選項。

我的控制器:

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) 
+0

在調試,你看看是否有任何的項目設置爲true:(sli.Text ==風格)? – VinnyG

回答

2

DropDownListFor,選擇=真不工作

是啊。

但是我不能在下拉列表中選擇一個指定的選項,第一個選項總是首先被選中。

當您使用

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

MVC忽略.selected,而是驗證在<IEnumerable<SelectListItem>>對值m.<MyId>值。

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

相關問題