淘汰赛js,mvc下拉默认值未选择 [英] Knockout js , mvc drop down default value is not selecting
本文介绍了淘汰赛js,mvc下拉默认值未选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这似乎很简单,但我无法做到.
it seems so simple but I am unable to do it.
我已经用来自c#的Json数据限制了一个下拉列表.
I have bounded a Dropdown list with Json data from c#.
查看页面
<div class="col-md-6">
<select data-bind="options: VehicleTypes, optionsText: 'Name', optionsValue:'ID', optionsCaption: 'All', value: VehicleTypeId" class="form-control"></select>
</div>
杰森(Json)呼叫
淘汰赛模型
var baseModel = {
VehicleTypeId: null,
VehicleTypes: [],
}
var viewModel = ko.mapping.fromJS(baseModel);
$.get(urlPath + "/GetVehicleTypes", function (data) {
viewModel.VehicleTypes(data);
});
它确实填满了下拉列表,当我在下拉列表中选择一个值时,会更新VehicleTypeId.
It does fill up the drop down and when I select a value in drop down value gets updated for VehicleTypeId.
我的问题是我想为下拉列表设置默认值,我已经用谷歌搜索过,但对我来说没有什么用
My problem is I want to set a default value for my drop down list, I have googled it but nothing is workign for me
我在Json中获得的数据如下.
Data I am getting back in Json is as follow.
[{
"ID": 32,
"Name": "Vehicle 1"
}, {
"ID": 30,
"Name": "Vehicle 2"
}, {
"ID": 31,
"Name": "Vehicle 3"
}]
推荐答案
您将不得不为VehicleTypeId分配一个值,以便设置默认值.
You will have to assign a value to the VehicleTypeId so that the Default value is set.
此处是一个有效的示例.
Here is a working example.
function VehicleTypesViewModel(){
var self = this;
self.VehicleTypeId = null;
self.VehicleTypes = [];
}
var viewModel = new VehicleTypesViewModel();
$.get(urlPath + "/GetVehicleTypes", function (data) {
viewModel.VehicleTypeId = 32;
viewModel.VehicleTypes = data;
ko.applyBindings(viewModel);
});
这篇关于淘汰赛js,mvc下拉默认值未选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文