2014-04-09 2 views
1

DropDownListFor를 사용하여 Select가 작동하지 않습니다. 누구든지 나를 도울 수 있습니까?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; } 
} 

내 secound 모델 :

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

답변

-1

아마 당신이 당신의 selec 목록 아이템이나 당신의 모델을 채우는 것과 관련이 있습니다.

당신은이 게시물에서 좀 걸릴 수 있습니다 :

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

+0

[나는 링크가 환상적이라고 생각하지만, 결코 당신의 대답에서 유일한 정보는 아닐 것입니다] (http://meta.stackexchange.com/a/8259/171858) –

2

DropDownListFor, 그래

작동하지 않습니다 = true를 선택했습니다.

그러나 나는 목록을 선택 드롭 다운에 지정된 하나의 옵션은 첫 번째 옵션은 항상 처음에 선택 할 수 없습니다.

당신이

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

관련 문제