模型绑定
您可以使用两者在DropDownList中实现模型绑定本地数据和远程数据。
本地数据
本地数据是初始化DropDownList时客户端上可用的数据。
通过将数据传递给视图
显示数据
。public ActionResult Index() {ViewData["products"] = GetProducts();return View(new ProductViewModel {ProductID = 4, ProductName = "ProductName4"});}私有静态IEnumerable
GetProducts() {var products = Enumerable。范围(0,2000)。Select(i => new ProductViewModel {ProductID = i, ProductName = "ProductName" + i});返回产品;} 将DropDownList添加到视图中,并将其绑定到保存在
显示数据
。@ model MvcApplication1.Models。ProductViewModel @ (Html.Kendo()。DropDownListFor(m => m.ProductID) .DataValueField("ProductID") .DataTextField("ProductName") .BindTo((System.Collections.IEnumerable)ViewData["products"]) )
远程数据
您可以配置DropDownList,使其通过发出AJAX请求从远程源获取数据。
创建一个将数据作为JSON结果返回的操作。
public ActionResult Index() {return View(new ProductViewModel {ProductID = 4, ProductName = "ProductName4"});}公共JsonResult GetProductsAjax() {var产品= Enumerable。范围(0,500)。Select(i => new ProductViewModel {ProductID = i, ProductName = "ProductName" + i});返回Json(products, JsonRequestBehavior.AllowGet);}
将DropDownList添加到视图并配置其DataSource以使用远程数据。
@ model MvcApplication1.Models。ProductViewModel @ (Html.Kendo()。DropDownListFor(m => m.ProductID) .Filter("contains") .DataTextField("ProductName") .DataValueField("ProductID") .OptionLabel("Select product...") .DataSource(source => { source.Read(read => { read.Action("GetProductsAjax", "Home"); }) .ServerFiltering(false); }) )