如何使用knockout.js将数据库中的数据绑定到下拉列表中 [英] how to bind data from database into dropdown using knockout.js
本文介绍了如何使用knockout.js将数据库中的数据绑定到下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将数据绑定到数据库的下拉列表中。我写了以下代码:
模型
Hi,
I need to bind data into a dropdown from my database. I have written the following code:
In Model
public static IQueryable<JOBCATEGORY> GetCategory()
{
var db = new TKTv2JobEntities();
var CategoryQry = from c in db.JOBCATEGORies.ToList()
select new JOBCATEGORY
{
CategoryId = c.CategoryId,
Category = c.Category
};
return CategoryQry.AsQueryable();
}
在API控制器中:
In API controller:
public IEnumerable<JOBCATEGORY> getCategory()
{
var category = HelpDeskRepository.GetCategory();
return category.ToList();
}
查看:
< select style =font-size:12px; font-weight:normal; name =select1id =select1data-bind =options:categoryList,optionsText:'Category',value:CategoryId,optionsCaption:... Select ...>< / select>
在js
In View:
<select style = "font-size: 12px; font-weight: normal;" name="select1" id="select1" data-bind="options: categoryList, optionsText: 'Category', value: CategoryId, optionsCaption: "...Select...""></select>
In js
self.categoryList=ko.observableArray([]);
$.getJSON("api/helpdeskapi/getCategory", function (data) {
$.each(data, function (key, val) {
self.categoryList.push();
});
});
推荐答案
.getJSON( api / helpdeskapi / getCategory ,function(data){
.getJSON("api/helpdeskapi/getCategory", function (data) {
.each(data,function(key,val){
self.categoryList.push();
} );
});
.each(data, function (key, val) { self.categoryList.push(); }); });
价值-binding.html [ ^ ]
查看knockout.js的文档..
value-binding.html[^]
check the documentation for knockout.js..
这篇关于如何使用knockout.js将数据库中的数据绑定到下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文