[英]Binding Issue using Knockout.js?
api using Knockout.js. 使用Knockout.js的api。 I want to bind dropdownlist dynamically.Here the code i used for knockout
我想动态绑定下拉列表。这里是我用于淘汰赛的代码
function LeadModel() {
var that = this;
that.Saleslist = ko.observableArray("") //Sales list is my model
}
function SalesEmpNm() {
var that = this;
that.LeadModel = new LeadModel();
that.reset = function () {
that.LeadModel.Salesid("");
that.LeadModel.SalesNme("");
};
that.submit = function () {
var json1 = ko.toJSON(that.LeadModel);
$.ajax({
url: '/api/values',
type: 'GET',
dataType: 'json',
data: json1,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var message = data.Message;
}
});
};
};
var _vm = new SalesEmpNm();
$(function () {
ko.applyBindings(_vm);
});
Here my text 这是我的文字
Sales Name: 销售名称:
<select id="ddlSales" name="ddlSales"
data-bind="options:$root.LeadModel.Saleslist, Value:'Salesid',
Text:'SalesNme', Value:LeadModel.Salesid">
</select>
Pleas suggest me the answer for binding dropdownlist dynamically. 请为我建议动态绑定下拉列表的答案。 Thanks & Regards
感谢和问候
i can't see where you are populating your Saleslist array. 我看不到您要在哪里填充Saleslist数组。 However, following should work in your case:
但是,以下情况适用于您的情况:
<select id="ddlSales" name="ddlSales"
data-bind='optionsCaption: "[Please Select]", options: _vm.LeadModel.Saleslist(), optionsText: "SalesNme", optionsValue: "Salesid"'></select>
Note that I am binding the array like _vm.LeadModel.Saleslist()
请注意,我绑定的数组类似于
_vm.LeadModel.Saleslist()
another issue in your models is LeadModel
does not have Salesid
and SalesNme
attributes which you are accessing in reset
function: 模型中的另一个问题是
LeadModel
不具有您要在reset
函数中访问的Salesid
和SalesNme
属性:
that.reset = function () {
that.LeadModel.Salesid("");
that.LeadModel.SalesNme("");
};
The issue might be you're initializing your ko.observableArray("")
with an empty string ""
, which isn't supported. 问题可能是您正在使用不支持的空字符串
""
初始化ko.observableArray("")
。 You might just need to change it to ko.observableArray()
. 您可能只需要将其更改为
ko.observableArray()
。
But as @Ahsan said, there might be other issues in your pasted code, such as the undefined Salesid
and SalesNme
, and your success
handler doesn't populate the Saleslist
as I would've expected. 但是正如@Ahsan所说,您粘贴的代码中可能还会存在其他问题,例如未定义的
Salesid
和SalesNme
,并且您的success
处理程序不会像我期望的那样填充Saleslist
。
Perhaps the issue is because you aren't providing any values in your Saleslist
. 可能是因为您没有在
Saleslist
提供任何值。
This would solve that issue: 这将解决该问题:
this.Saleslist = ko.observableArray([
{ Salesid: 1, SalesNme: "One" },
{ Salesid: 2, SalesNme: "Two" }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.