ASP.NET Knockout依赖下拉列表在回发时无法获取选定值

时间:2016-08-18 05:27:51

标签: asp.net knockout.js

我有一个带有淘汰赛2.3.0的asp.net页面。该页面包含一个从属下拉列表。在example.js中,它首先列出了这两个下拉列表。依赖列表正在运行。

但是当我在前端选择了值时,在服务器端点击按钮时,我无法获得选择的值。所选值始终设置为默认值。

请帮忙!

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" EnableEventValidation="false" Inherits="WebApplication1.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="JS/knockout-2.3.0.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>

    <asp:DropDownList runat="server" ID="ddl1" data-bind="options: jobAreas, optionsCaption:'--Job Areas--', optionsText: 'jobAreaName', optionsValue: 'jobAreaID', value: selectedJobArea">
    </asp:DropDownList>
    <asp:DropDownList runat="server" ID="ddl2" data-bind="options: jobFunctions, optionsCaption:'--Job Function--', optionsText: 'jobFunctionName', optionsValue: 'jobFunctionID', value: selectedJobFunction, enable: jobFunctions().length">
    </asp:DropDownList>

    <asp:LinkButton ID="Button1" runat="server" Text="Next" OnClick="Test" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptTags" runat="server">
    <script src="JS/example.js"></script>
</asp:Content>

在example.js中

    function ViewModel(items) {
    this.jobAreas = ko.observableArray(items);
    this.selectedJobArea = ko.observable();
    this.selectedJobFunction = ko.observable();

    function getById(items, value) {
        if(!value) {
            return [];
        }

        var result = ko.utils.arrayFirst(items, function(item) {
            return item.jobAreaID === value;
        });

        return result && result.JubfunctionItems || [];
    }

    this.jobFunctions = ko.computed(function(){
        var items = this.jobAreas();
        var id = this.selectedJobArea();
        return getById(items, id);
    }, this);

}

var items = [
    { jobAreaName: 'Ford', jobAreaID: 1, JubfunctionItems:
     [
         { jobFunctionName: 'F-150', jobFunctionID: 1},
         { jobFunctionName: 'F-250', jobFunctionID: 2}
     ]
    },
    { jobAreaName: 'Honda', jobAreaID: 2, JubfunctionItems:
     [
         { jobFunctionName: 'Civic', jobFunctionID: 5},
         { jobFunctionName: 'Accord', jobFunctionID: 6}
     ]
    }
];

var module = {};

module.viewModel = new ViewModel(items);

ko.applyBindings(module.viewModel);

1 个答案:

答案 0 :(得分:1)

下拉列表在您的aspx页面中被声明为空,并将被记录在ViewState中。

通过javascript生成的任何下拉列表项都不会被识别为有效项,也不会记录在.SelectedValue

但是,您可以通过直接访问Sub OpenFileFolder() Dim WBA As Workbook 'Opened Workbook Dim FilePath As String Dim TestStr As String Dim FileExtension As String Dim lastRow As Long Dim Rng As Range Application.AskToUpdateLinks = False Application.ScreenUpdating = False FilePath = "C:\Users\anthonyer\Documents\Automation VBA\Source\Comcast Secondary\" FilePath = "C:\" ' can modify it to filter only Excel files FileExtension = "*" TestStr = "" On Error Resume Next TestStr = Dir(FilePath & FileExtension) On Error GoTo 0 ' file found If Len(TestStr) > 0 Then Set WBA = Workbooks.Open(Filename:=FilePath & TestStr) WBA.Application.CutCopyMode = False ' find last row in Column B in WBA Sheets(1) lastRow = WBA.Sheets(1).Cells(WBA.Sheets(1).Rows.Count, "B").End(xlUp).Row ' Set Range of cells to copy Set Rng = WBA.Sheets(1).Range("B2:B" & lastRow) Rng.Copy Destination:=ThisWorkbook.Sheets(1).Range("A" & ThisWorkbook.Sheets(1).Cells(ThisWorkbook.Sheets(1).Rows.Count, "A").End(xlUp).Row + 1) WBA.Close (False) ThisWorkbook.Activate Worksheets("Data Tracker").Range("A2").Value = "Complete" Else ' file not found Workbooks("FullAuto Final.xlsm").Worksheets("Data Tracker").Range("B2").Value = "Missing" End If Application.AskToUpdateLinks = True Application.ScreenUpdating = True End Sub 来访问所选值。例如:

Request.Form

Request.Form[ddl2.UniqueID]