Javascript MVC淘汰赛。儿童下拉列表不';t第一次加载模型数据
型号:Javascript MVC淘汰赛。儿童下拉列表不';t第一次加载模型数据,javascript,asp.net-mvc,knockout.js,Javascript,Asp.net Mvc,Knockout.js,型号: public class StoreData { public SelectList Regions { get; set; } public SelectList Groups { get; set; } } function CascadingDDLViewModel() { this.Groups = ko.observableArray([]); } var objVM = new CascadingDDLViewModel(); ko.applyBi
public class StoreData
{
public SelectList Regions { get; set; }
public SelectList Groups { get; set; }
}
function CascadingDDLViewModel() {
this.Groups = ko.observableArray([]);
}
var objVM = new CascadingDDLViewModel();
ko.applyBindings(objVM);
@Html.DropDownList("gid", (SelectList)Model.Groups, new { id = "ddlGroup", data_bind = "options: Groups, optionsText: 'Text', optionsValue: 'Value'" })
客户端:
public class StoreData
{
public SelectList Regions { get; set; }
public SelectList Groups { get; set; }
}
function CascadingDDLViewModel() {
this.Groups = ko.observableArray([]);
}
var objVM = new CascadingDDLViewModel();
ko.applyBindings(objVM);
@Html.DropDownList("gid", (SelectList)Model.Groups, new { id = "ddlGroup", data_bind = "options: Groups, optionsText: 'Text', optionsValue: 'Value'" })
HTML:
public class StoreData
{
public SelectList Regions { get; set; }
public SelectList Groups { get; set; }
}
function CascadingDDLViewModel() {
this.Groups = ko.observableArray([]);
}
var objVM = new CascadingDDLViewModel();
ko.applyBindings(objVM);
@Html.DropDownList("gid", (SelectList)Model.Groups, new { id = "ddlGroup", data_bind = "options: Groups, optionsText: 'Text', optionsValue: 'Value'" })
Jquery用于加载下拉列表
$("#ddlGroup").change(function () {
var ddl = this;
var id = ddl.options[ddl.selectedIndex].value;
var url = $(ddl).attr("data-url");
$.getJSON(url, { id: id }, function (data) { objVM.Stores(data); });
});
在从属下拉列表中加载数据后的第一时间,数据不存在。选择父列表中的项目后,一切正常
重新加载页面后出现问题。页面加载后如何绑定数据 我只用一行就解决了问题
$('#ddlRegion').trigger('change');
现在一切正常。但我有一个问题。现在我还不完全明白。加载后如何使用MVC模型来设置默认数据。信息技术数据是使用依赖关系生成的
StoreData store = new StoreData();
store.Regions = _dataManager.Monitorings.GetRegionsSelectList(Region_id, true);
store.Region_id = (Region_id == -1 ? (int)store.Regions.SelectedValue : Region_id);
store.Groups = _dataManager.Monitorings.GetStoreGroupsSelectList(store.Region_id, Group_id);
store.Group_id = Group_id;
我之前使用的方法是:
@Html.DropDownList("sid", (SelectList)Model.Stores, new { id = "ddlStore" })
现在:
我知道必须设置客户端模型数据。但我不明白怎么做?
现在数组为空:
this.Groups = ko.observableArray([]);
你用过jquery吗…??是的。用于更新数据:$(“#ddlGroup”).change(函数(){var ddl=this;var id=ddl.options[ddl.selectedIndex].value;var url=$(ddl.attr(“数据url”);$.getJSON(url,{id:id},函数(数据){objVM.Stores(数据);});我认为您没有很好地理解MVVM的概念。您不应该将事件处理程序绑定到DOM。这没有多大意义。@Blackraven我建议你在上完成教程。您可能会更好地掌握MVVM的概念,而且您还将看到一些关于如何使用knockout处理下拉列表的示例。