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)
, 어떤 항목이 true로 설정되어있는 경우를 참조하십시오 (sli.Text == 장르)? – VinnyG