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地图