2011-12-27 217 views
4

我即將在MVC 3中創建級聯下拉列表。Logik很簡單,對於每個位置可能有很多員工,因此我可以找出如何實現它的最佳方式是下拉列表。級聯下拉MVC 3

我有什麼現在的問題是:

1清單在與位置begninnging填滿。和變化,我可以當我嘗試發送員工在與ID和名稱沒有什麼happend數據放入第二個我的員工名單,但我傳送的數據是Enumrable日期」

控制器代碼:

public ActionResult Months(string locId) 
{ 
    var k = service.getEmployeeForLocation(Int32.Parse(locId)) 
       .ToList() 
       .Select(x => new 
         { 
          Value = .EmployeeId, 
          Text = x.Name 
         }); 

    return Json(k,JsonRequestBehavior.AllowGet); 
} 

查看:

<tr> 
<td>Choose your closest location : 
    @Html.DropDownListFor(x => x.SelectedLocation, Model.Locations)</td> 
<td>Choose your closest location : 
    @Html.DropDownListFor(x => x.SelectedEmployee, 
          Enumerable.Empty<SelectListItem>(), "-- select month --") 
</tr> 

的Javascript

</script> 
<script type="text/javascript"> 
    $('#SelectedLocation').change(function() { 
     var selectedLocation = $(this).val(); 
     if (selectedLocation != null && selectedLocation != '') { 
      $.getJSON('@Url.Action("Months")', { locId: selectedLocation }, 
      function (employee) { 

       var EmployeeSelect = $('#SelectedEmployee'); 
       //    monthsSelect.empty(); 

       $.each(employee, function (index, employee) { 
        EmployeeSelect.append($('<option/>', { 
         value: employee.value, 
         text: employee.text 
        })); 
       }); 
      }); 

     } 
    }); 
</script> 

回答

2

我可以看到的一件事是您的操作中的變量名以大寫字母開頭,JavaScript區分大小寫。在你的頁面中,你使用的是小寫字母,但如果你想將外殼保存在控制器中,它們應該是大寫字母。

+0

哦,我的上帝....我總是忘記JS對我自己是大小寫敏感的.../facepalm。非常感謝。我願意在VS – Timsen 2011-12-27 01:22:55

+0

@Timsen沒有問題,節日快樂! – Craig 2011-12-27 01:44:15