2017-12-08 1 views
2

현재 두 개의 테이블 (팀 및 직원)이 있습니다 팀의 dropdownList를 완벽하게 채우고 있습니다. 다음으로 Employees 팀의 selectedId에 따라 두 번째 드롭 다운 목록을 채우려고합니다.ASP.NET MVC의 종속 드롭 다운 목록

컨트롤러 :

// GET: CalView 
     public ActionResult Index(string ses, string DH) 
     {  //Team Lead Members 
       var eID = Convert.ToInt32(Session["currentEmployeeID"]); 
       var EmpID = Session["currentEmpID"]; 
       Employee obj = (from o in db.Employees 
           where o.EnrollNumber == EmpID 
           select o).FirstOrDefault(); 

       Department dept = (from dep in db.Departments 
            where dep.LeadBy == obj.EmployeeId 
            select dep).FirstOrDefault(); 
       //this works fine 
       ViewBag.showTeams = new SelectList(db.Teams.Where(tm => (tm.DeptID == dept.DepartmentId) && (dept.LeadBy == eID)), "TeamID","Name"); 
       //this obviously does not 
       ViewBag.showMembers = new SelectList(db.Employees.Where(empt => (empT.TeamID == selectedIdFromPreviousDropDownList), "EmployeeID", "Employee")); 

       return View(); 
     } 

보기 :

if ((Session["UT"] == "DD") && (@ViewBag.DeptLead != null)) 
    { 
//this works 
    @Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { @class = "form-control" }) 
//this does not work 
    @Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new { @class = "form-control" }) 
    } 

가 좀 AJAX 호출이 필요하십니까? 또는 아마 POST 메서드? MVC를 처음 접했습니다.

+0

예, 최선의 방법은 ** Ajax ** 호출, 당신은 JQuery를 사용하고 있습니까? –

+0

가능한 [MVC 3 C#] 목록의 계단식 복제본 (https://stackoverflow.com/questions/10016560/cascading-dropdown-lists-mvc-3-c-sharp) –

+0

[this DotNetFiddle]의 코드 연구를 제안하십시오. (https://dotnetfiddle.net/1bPZym)을 사용하여 계단식 드롭 다운 목록을 구현하는 방법 이해 –

답변

1

AJAX 전화가 필요합니까? 또는 아마 POST 메서드?

@Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { id = "ddshowTeams", @class = "form-control" }) 
@Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new {id = "ddshowMembers", @class = "form-control" }) 

바로 거기 jsonResult 기능, GetMembers 어떤 마법을 만듭니다 : 일부 아이디 아마의 당신의 DropdownLists 줘

: 좋아 다음에,이 방법을 수행 할 수 있습니다

<script type="text/javascript"> 

     $(document).ready(function() { 
      //Dropdownlist Selectedchange event 
      $("#ddshowTeams").change(function() { 
       console.log("pehla andar"); 
       $("#ddshowMembers").empty(); 
       $.ajax({ 
        type: 'POST', 
        url: '@Url.Action("GetMembers")', 
        dataType: 'json', 
        data: { id: $("#ddshowTeams").val() }, 
        success: function (mems) { 
         console.log("wich ayaeee"); 
         // states contains the JSON formatted list 
         // of states passed from the controller 
         $.each(mems, function (i, member) { 
          $("#ddshowMembers").append('<option value="' 
    + member.Value + '">' 
    + member.Text + '</option>'); 
         }); 
        }, 
        error: function (ex) { 
         alert('Failed to retrieve states.' + ex); 
        } 
       }); 
       return false; 
      }) 
     }); 
</script> 

과에 당신의 컨트롤러 :

public JsonResult GetMembers(int id) 
     { 
      return Json(new SelectList(db.Employees.Where(empt => (empt.TeamId == id)), "EmployeeID", "FirstName")); 
     }