1

드롭 다운 목록에서 선택한 값에 문제가 있습니다.드롭 다운 목록에서 값을 true로 선택하는 방법 MVC

보기

@Html.DropDownList("QuestionType", (IEnumerable<SelectListItem>)ViewData["questiontype"], new { id = "QuestionType", @style = "max-width: 200px;width: 200px", @OnChange = "HideForm(this)", @class = "form-control" }) 

컨트롤러

var questionTypeList = new SelectList(
        new List<SelectListItem> 
        { 
         new SelectListItem { Value = "1", Text = "Automatic", Selected = false }, 
         new SelectListItem { Value = "2", Text = "Custom", Selected = true} 
        }, "Value", "Text" 
       ).ToList(); 
var questionType = new SelectList(questionTypeList, "Value", "Text"); 
ViewData["questiontype"] = questionType; 

내가 값 = 2가보기에 진정한 선택하지만, 위의 내 스크립트가 작동하지 않습니다 싶지는이 문제를 어떻게 해결할 수 있습니까?

내 코드가 수정되었습니다. 나는 droptlist에 bind하는 strongtype을 사용하지 않는다. 나는 viewdata를 사용하고있다.

컨트롤러

List<SelectListItem> questionTypeList = new List<SelectListItem>(); 
      questionTypeList.Add(new SelectListItem() { Text = "Automatic", Value = "1" }); 
      questionTypeList.Add(new SelectListItem() { Selected = true, Text = "Custom", Value = "2" }); 
ViewData["questiontype"] = questionTypeList; 

보기

@Html.DropDownList("QuestionType", ViewData["questiontype"] as SelectList, new { id = "QuestionType", @style = "max-width: 200px;width: 200px", @OnChange = "HideForm(this)", @class = "form-control" }) 

코드가 작동하는 이유는 앞의 코드가 작동하지 왜이 내 코드? 나는 그래서 당신은 강력한 형식의 뷰를 가지고 있지만 경우 컨트롤러가 다음

return View(model); 

model.QuestionType to 2; 

설정에

답변

1

없음 당신이 다음

DropdownlistFor(model=>model.QuestionType, selectlistItem) 

을 사용할 수 있습니다 ... 혼동하고있어

+0

죄송합니다 .. 강력하게 형식화 된보기를 사용하고 있지 않습니다. 난 단지 드롭 다운 목록에 바인딩 viewdata를 사용합니다. 나는 나의 질문을 편집했다, 나는 (작업이 아닌) 그리고 수정 된 (작업) 전에 나는 너무 혼란 스럽다. 도와 줄 수 있어요? –

관련 문제