Knockout Js 另一个javascript库。 开源,
纯Javascript,小,无靠,支持广大浏览器。在Asp.net
MVC中我们来贯彻一个大概的级联下拉列表。
先看我们定义的Controller与Model:

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            ViewBag.Country = new SelectList(Country.GetCountryList(), "Code", "Name");

            return View();

        }

 

        public ActionResult GetStates(string id = "")

        {

            var stateList = State.GetStates()

                .Where(s => s.CountryCode.ToLower() == id.ToLower());

 

            return this.Json(stateList, JsonRequestBehavior.AllowGet);

        }

    }

 

    public class Country

    {

        public string Code { get; set; }

        public string Name { get; set; }

 

        public static List<Country> GetCountryList()

        {

            return new List<Country>

            {

                new Country { Code = "IN", Name = "India" },

                new Country { Code = "US", Name = "United State" },

                new Country { Code = "UK", Name = "United Kingdom" }

            };

        }

    }

 

    public class State

    {

        public string CountryCode { get; set; }

        public int StateId { get; set; }

        public string StateName { get; set; }

 

        public static List<State> GetStates()

        {

            int stateId = 0;

            return new List<State>

            {

                new State { CountryCode = "CN", StateId = stateId++, StateName = "ShangHai" },

                new State { CountryCode = "CN", StateId = stateId++, StateName = "BeiJing" },

                new State { CountryCode = "CN", StateId = stateId++, StateName = "HongKong" },

                new State { CountryCode = "US", StateId = stateId++, StateName = "New York" },

                new State { CountryCode = "US", StateId = stateId++, StateName = "California" },

                new State { CountryCode = "US", StateId = stateId++, StateName = "Washington" },

                new State { CountryCode = "UK", StateId = stateId++, StateName = "London" },

                new State { CountryCode = "UK", StateId = stateId++, StateName = "Scotland" },

                new State { CountryCode = "UK", StateId = stateId++, StateName = "Britian" }

            };

        }

    }

留意这里是以演示,在MODEL填充的数码。GetStates用于Ajax请求的Action。在前端的cshtml中,类似这样的有些:

<p>

    <b>Select Country :</b> @Html.DropDownList("ddlCountry", ViewBag.Country as SelectList, "Select...", new { onchange = "FetchStates();" })

</p>

<p data-bind="visible: states().length > 0">

    <b>Select State :</b>

    <select data-bind="options: states, optionsText: 'StateName', optionsValue: 'StateId', optionsCaption: 'Choose...'">

    </select>

</p>

上面下visible的API,从此处可以望用于控制是否出示。参考官方的Visible
Binding 的
API文档。
接着是冲select标签options
binding。
在View中Javascript定义我们的ViewModel :

function CascadingDDLViewModel() {

    this.states = ko.observableArray([]);

}

 

var objVM = new CascadingDDLViewModel();

ko.applyBindings(objVM);

 

咱们看到地方的代码中,我们创建ko.observableArray类型states属性的ViewModel.knockoutjs中之Observable类型自动通知当它们的对象被更新。
此地以乘的data-bind特性,之前文章中发生取了它们,用knockoutjs的Viewmodel来促成绑定DOM元素行为。无论ViewModel被更新,同时DOM元素呢会见给自动更新.

function FetchStates() {

    var countryCode = $("#ddlCountry").val();

    $.getJSON("/Home/GetStates/" + countryCode, null, function (data) {

        objVM.states(data);

    });

}

当下拉列表改变时,我们用jQuery.getJSON方法去取数量。在请成功后更新ViewModel,而不待手动用Javascript做字符串操作来兑现一个DropDownList.

你或许感兴趣之章:

Jquery实现无刷新DropDownList联动
Html
5丁打定义data-*特性

 

作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
正文版权jQuery归作者和博客园共有,欢迎转载,但未经作者同意要保留这个段子声明,且当篇章页面明显位置被起原文连接,否则保留追究法律责任的权。
该文章也以揭晓以我之独门博客中-Petter Liu
Blog。

相关文章

网站地图xml地图