问题描述:

I'm having problem with my cascading dropdownlist (DDL), it perfectly works fine when you choose the first DDL and when you select the second DDL the third DLL do not fire and the value is null.

Here's my 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)

);

});

});

});

});

View

@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>

Model

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

Controller

 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;

}

网友答案:

It's because you are targeting the wrong element:

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

when you should have done this:

$('#Floor_SelectedItem').change(function () {
网友答案:

$.getJSON('@Url.Action("ActionFloor")'.... I dont think Url.Action will work like this either...

相关阅读:
Top