简体   繁体   English

我的项目中选择的项目 <select>在我的淘汰赛控制器中为空

[英]item selected in my <select> is empty in my knockout controller

Pretty new to knockout, so it might be pretty obvious, but I have a hard time finding specific information about that. 淘汰赛还很新,所以可能很明显,但是我很难找到有关此的特定信息。

I try to make cascading dropdown. 我尝试使级联下拉。 The first select is populated server side, not by Knockout, so I thought I just had to bind it like this : 第一个选择是在服务器端填充的,而不是通过Knockout填充的,所以我认为我只需要这样绑定它:

<select 
    data-bind="value: CompanyId" 
    id="CompanyId" 
    name="CompanyId" 
    onchange="GetEmployees();">

    <option value="">...</option>
    ...
</select>

Then, in my controller : 然后,在我的控制器中:

function CascadingDdLViewModelEmployeeEdit() {
    this.EmployeeList = ko.observableArray([]);
}

var objVMEmployeeEdit = new CascadingDdLViewModelEmployeeEdit();
ko.applyBindings(objVMEmployeeEdit);

function GetEmployees() {
    objVMEmployeeEdit.EmployeeList([]);
    var CompanyId = $("#CompanyId").val(); // EMPTY
    // ...
    });
}

But $("#CompanyId").val() is empty. 但是$(“#CompanyId”)。val()为空。 Where am I wrong ? 我哪里错了?

EDIT 编辑

Ok, I got it. 好,我知道了。 The value databinding creates a variable with the same name (CompanyId), and I just need to use it. 值数据绑定创建一个具有相同名称(CompanyId)的变量,我只需要使用它即可。

function GetEmployees() {
    objVMEmployeeEdit.EmployeeList([]);
    var route = "/Company/GetEmployees/" + CompanyId;
    // ...
    });
}

But now I get the previously selected value, not the one that triggered the event. 但是现在我得到以前选择的值,而不是触发事件的值。

Response to your edit, try following - in viewmodel make a subscription instead of using onchange event: 响应您的编辑,请尝试以下操作-在viewmodel中进行订阅,而不要使用onchange事件:

CompanyId.subscribe(function (newValue){
  GetEmployees(newValue);
}

And change GetEmployees to receive new value and use that instead of CompanyId. 并将GetEmployees更改为接收新值,并使用它代替CompanyId。

function GetEmployees(newValue) {
    objVMEmployeeEdit.EmployeeList([]);
    var route = "/Company/GetEmployees/" + newValue;
    // ...
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM