2013-04-04 61 views
1

我有我的级联下拉列表(DDL)的问题,它完美地工作正常,当你选择第一个DDL,当你选择第二个DDL第三个DLL不会触发和价值一片空白。为什么我的级联下拉列表不起作用? (ASP.NET [MVC 3])

这里是我的JS

$('#Building_SelectedBuildingID').change(function() { 
     var SelectedBuildingID = $(this).val(); 
     $.getJSON('@Url.Action("ActionFloor")', { buildingId: SelectedBuildingID }, function (oFloors) { 
      var SelectFloorID = $('#Floor_SelectedItem'); 
      SelectFloorID.empty(); 
      $.each(oFloors, function (index, Floor) { 
       SelectFloorID.append(
        $('<option/>') 
         .attr('value', Floor.Code) 
         .text(Floor.Description) 
       ); 
      }); 
     }); 
    }); 

    $('#Floor_SelectedFloorID').change(function() { 
     var SelectedBuildingID = $('#Building_SelectedBuildingID').val(); 
     var selectedFloorId = $(this).val(); 
     $.getJSON('@Url.Action("ActionLocation")', { buildingId: SelectedBuildingID,floorId: selectedFloorId }, function (oLocations) { 
      var SelectLocationID = $('#Location_SelectedItem'); 
      SelectLocationID.empty(); 

      $.each(oLocations, function (index, Location) { 
       SelectLocationID.append(
        $('<option/>') 
         .attr('value', Location.Code) 
         .text(Location.Description) 
       ); 
      }); 
     }); 
    }); 
}); 

查看

@model FA_CS.Models.Operation.NewRegistration 
<table style="width:100%;"> 
    <tr> 
     <td colspan="2"> 
      <strong>Location</strong></td> 
    </tr> 
    <tr> 
     <td class="style1"> 
      @Html.LabelFor(m => m.Building)</td> 
     <td> 
      @Html.DropDownListFor(x => x.Building.SelectedBuildingID, new SelectList(Model.Building.BuildingItems, "code", "description")) 
      </td> 
    </tr> 
    <tr> 
     <td class="style1"> 
      @Html.LabelFor(m => m.Floor) </td> 
     <td> 
      @Html.DropDownListFor(x => x.Floor.SelectedItem, Enumerable.Empty<SelectListItem>()) 
       </td> 
    </tr> 
    <tr> 
     <td class="style1"> 
      @Html.LabelFor(m => m.Location)</td> 
     <td> 
      @Html.DropDownListFor(x => x.Location.SelectedItem, Enumerable.Empty<SelectListItem>()) 
      </td> 
    </tr> 
</table> 

型号

Imports System 
Imports System.Collections.Generic 
Imports System.ComponentModel 
Imports System.ComponentModel.DataAnnotations 
Imports System.Globalization 
Public Class ItemClass 
    Private _code As String 
    Private _description As String 
    Private _ClassItems As System.Collections.Generic.List(Of ItemClass) 
    Private _Type As MDL.Type 
    Private _Types As System.Collections.Generic.List(Of MDL.Type) 
    Private _SelectedClassItem As String 
    Private _SelectedTypeItem As String 
    Private _SelectedSubTypeItem As String 



    Public Property SelectedClassID As String 
     Get 
      Return _SelectedClassItem 
     End Get 
     Set(value As String) 
      _SelectedClassItem = value 
     End Set 
    End Property 

    Public Property SelectedTypeID As String 
     Get 
      Return _SelectedTypeItem 
     End Get 
     Set(value As String) 
      _SelectedTypeItem = value 
     End Set 
    End Property 

    Public Property SelectedSubTypeID As String 
     Get 
      Return _SelectedSubTypeItem 
     End Get 
     Set(value As String) 
      _SelectedSubTypeItem = value 
     End Set 
    End Property 

    <DataType(DataType.Text)> _ 
    <Display(Name:="Type")> _ 
    Public Property code() As String 
     Get 
      Return _code 
     End Get 
     Set(ByVal Value As String) 
      _code = Value 
     End Set 
    End Property 
    <DataType(DataType.Text)> _ 
    <Display(Name:="Type")> _ 
    Public Property description() As String 
     Get 
      Return _description 
     End Get 
     Set(ByVal Value As String) 
      _description = Value 
     End Set 
    End Property 

    Public Property ClassItems() As System.Collections.Generic.List(Of ItemClass) 
     Get 
      Return _ClassItems 
     End Get 
     Set(ByVal value As System.Collections.Generic.List(Of MDL.ItemClass)) 
      _ClassItems = value 
     End Set 
    End Property 
    ''' <summary> 
    ''' Property for collection of Type 
    ''' </summary> 
    ''' <pdGenerated>Default opposite class collection property</pdGenerated> 
    Public Property Type() As MDL.Type 
     Get 
      Return _Type 
     End Get 
     Set(ByVal value As MDL.Type) 
      _Type = value 
     End Set 
    End Property 
    Public Property Types() As System.Collections.Generic.List(Of Type) 
     Get 
      Return _Types 
     End Get 
     Set(ByVal value As System.Collections.Generic.List(Of Type)) 
      _Types = value 
     End Set 
    End Property 
    Public Overrides Function ToString() As String 
     Return Me._description 
    End Function 


End Class 

有限公司ntroller

public JsonResult GetCascadeBuildings() 
    { 
     return Json(FA_CS.Helpers.BuildingList.Status, JsonRequestBehavior.AllowGet); 
    } 

    public JsonResult GetCascadeFloors(string buildingCode) 
    { 
     var gathererS = new List<MDL.Floor>(); 
     gathererS = GetFloors(buildingCode); 
     return Json(gathererS, JsonRequestBehavior.AllowGet); 
    } 

    public List<MDL.Floor> GetFloors(string BuildingCode) 
    { 
     FAWebService.Service1 faws = new FAWebService.Service1(); 
     Array arr = faws.GatherFloor(BuildingCode); 
     MDL.Floor oFloor = new MDL.Floor(); 
     List<MDL.Floor> oFloors = new List<MDL.Floor>(); 
     foreach (FAWebService.Floor itm in arr) 
     { 
      oFloors.Add(new MDL.Floor { SelectedItem = itm.Code, Description = itm.Description }); 
     } 
     return oFloors; 

    } 


    public JsonResult GetCascadeLocations(string buildingCode, string floorCode) 
    { 
     var gatherer = new MDL.Location(); 
     var gathererS = new List<MDL.Location>(); 
     gathererS = GetLocations(buildingCode, floorCode); 
     return Json(gathererS, JsonRequestBehavior.AllowGet); 
    } 

    public List<MDL.Location> GetLocations(string BuildingCode, string floorCode) 
    { 
     FAWebService.Service1 faws = new FAWebService.Service1(); 
     Array arr = faws.GatherLocation(BuildingCode, floorCode); 
     MDL.Location oLocation = new MDL.Location(); 
     List<MDL.Location> oLocations = new List<MDL.Location>(); 
     foreach (FAWebService.Location itm in arr) 
     { 
      oLocations.Add(new MDL.Location { Code = itm.Code, Description = itm.Description }); 
     } 
     return oLocations; 

    } 

回答

1

这是因为你的目标错误的元素:

$('#Floor_SelectedFloorID').change(function() { 

时,你应该这样做:

$('#Floor_SelectedItem').change(function() { 
+0

赞赏@von v。它现在是W工作会有。 – user2239718 2013-04-04 08:21:03

-1

$ .getJSON(“@ Url.Action(” ActionFloor“)'.... 我不认为Url.Action会这样工作...

+0

感谢@Lakshay的回应,但V.Von的建议解决了我的困境。 – user2239718 2013-04-04 08:22:30

+0

-1'$ .getJSON('@ Url.Action(“ActionFloor”)''始终工作 – 2013-04-04 08:46:35