2017-02-17 2 views
0

이 선택이 있고 DropDownlistFor 호출로 변환해야합니다.변환 대상 선택 DropDownListFor MVC 5

사람이 도와 줄 수 : 여기

<select class="form-control" name="QuestionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID" id="@selectId"> 
    @foreach (var x in questionBasicSection.Questions[j].LookupGroup) 
    { 
     <option value="@x.Value" selected="@x.Selected">@x.Text</option> 
    } 
</select> 

이 골격이다, 난 그냥 모르겠어요 설정하는 방법 selectList의와 소스를 제공함으로써 작동 DropDownListFor 선택한 값

@Html.DropDownListFor(q => questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID, 
      new SelectList(
        new List<Object>{ 
         new { value = 0 , text = "Red" }, 
         new { value = 1 , text = "Blue" }, 
         new { value = 2 , text = "Green"} 
        }, 
        "value", 
        "text", 
        // Not sure how to set selected 
      ) 

답변

0

/에 destination 속성을 첫 번째 매개 변수로, 옵션 배열을 두 번째 매개 변수로 사용합니다. 당신의 예에서 :

questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID

은 모델 속성은 DropDownList로가로 설정하고자하는 초기 값을 포함해야 함. 예를 들어, 페이지로드시 DropDownList에 'Blue'를 표시하려면 컨트롤러에서 (또는 모델이 생성 된 곳) ANSWER_LOOKUP_OPTION_ID의 값을 2로 설정하십시오.