2017-01-20 8 views
0

모든 데이터입니다.로드 내가 잘 작동 아래의 코드를 가지고있는 DropDownList

샘플 출력

컴퓨터학과

기술부

제품 부서

하자 내 부서 Product Department 말한다. 나는 system/project에 로그인 한 사람입니다. 내 부서를 먼저 DropDownList에로드 할 수 있습니까? 첫 번째로드에서의 의미는 내 부서가 첫 번째로 설정된 드롭 다운로드시입니다.

컨트롤러

ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentDesc", null).OrderBy(m => m.Text); 

HTML 새로운 selectList의에서 널 (null)로 설정

@Html.DropDownList("DepartmentId ", ViewData["DepartmentId "] as SelectList, "", new { @style = "width:130px;" }) 
+0

, 그 옵션이 선택됩니다. –

+0

@StephenMuecke 속성 값은 어떻게 설정합니까? – KiRa

+0

참조 [이 답변] (http://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) –

답변

0

는 선택한 옵션을 설정합니다.

ViewBag.DepartmentId = new SelectList(db.Departments.OrderBy(m => m.Text), "DepartmentId", "DepartmentDesc", selectedDepartmentId); 
0

이 코드를 사용해보십시오 : -

ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentDesc",SelectedDepartmentId).OrderBy(m => m.Text); 

그리고 당신이보기에서 모델을 통과하기 전에 그냥 GET 방법에 property`DepartmentId`의 값을 설정

@Html.DropDownList("DepartmentId ", ViewData["DepartmentId "] as SelectList, "My Department", new { @style = "width:130px;" })