为什么我的级联下拉列表无效? (ASP.NET [MVC 3])

时间:2013-04-04 07:55:02

标签: jquery ajax asp.net-mvc json asp.net-mvc-3

我的级联下拉列表(DDL)有问题,当你选择第一个DDL时,它完全正常工作,当你选择第二个DDL时,第三个DLL不会触发,值为null。

这是我的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

控制器

  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;

    }

2 个答案:

答案 0 :(得分:1)

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

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

你什么时候应该这样做:

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

答案 1 :(得分:-1)

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