首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的级联下拉列表不起作用?(ASP.NET [MVC 3])

为什么我的级联下拉列表不起作用?(ASP.NET [MVC 3])
EN

Stack Overflow用户
提问于 2013-04-04 07:55:01
回答 2查看 900关注 0票数 1

我的级联下拉列表( DDL )有问题,当您选择第一个DDL,选择第二个DDL时,第三个DLL不触发,值为null时,它非常好。

,这是我的JS

代码语言:javascript
复制
$('#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)
                );
            });
        });
    });
});

视图

代码语言:javascript
复制
@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>

模型

代码语言:javascript
复制
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

控制器

代码语言:javascript
复制
  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;

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-04 08:03:04

因为你瞄准了错误的元素:

代码语言:javascript
复制
$('#Floor_SelectedFloorID').change(function () {

当你应该这么做的时候:

代码语言:javascript
复制
$('#Floor_SelectedItem').change(function () {
票数 1
EN

Stack Overflow用户

发布于 2013-04-04 08:13:12

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15805504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档