ایجاد Drop Down List های آبشاری توسط Kendo UI
نویسنده: وحید نصیری
تاریخ: ۱۳۹۴/۰۱/۱۸ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Category
{
public int CategoryId { set; get; }
public string CategoryName { set; get; }
[JsonIgnore]
public IList<Product> Products { set; get; }
}
public class Product
{
public int ProductId { set; get; }
public string ProductName { set; get; }
} using System.Linq;
using System.Text;
using System.Web.Mvc;
using KendoUI12.Models;
using Newtonsoft.Json;
namespace KendoUI12.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(); // shows the page.
}
[HttpGet]
public ActionResult GetCategories()
{
return new ContentResult
{
Content = JsonConvert.SerializeObject(CategoriesDataSource.Items),
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
}
[HttpGet]
public ActionResult GetProducts(int categoryId)
{
var products = CategoriesDataSource.Items
.Where(category => category.CategoryId == categoryId)
.SelectMany(category => category.Products)
.ToList();
return new ContentResult
{
Content = JsonConvert.SerializeObject(products),
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
}
}
} <!--نحوهی راست به چپ سازی -->
<div class="k-rtl k-header demo-section">
<label for="categories">گروهها: </label><input id="categories" style="width: 270px" />
<label for="products">محصولات: </label><input id="products" disabled="disabled" style="width: 270px" />
</div>
@section JavaScript
{
<script type="text/javascript">
$(function () {
$("#categories").kendoDropDownList({
optionLabel: "انتخاب گروه...",
dataTextField: "CategoryName",
dataValueField: "CategoryId",
dataSource: {
transport: {
read: {
url: "@Url.Action("GetCategories", "Home")",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
}
}
}
});
$("#products").kendoDropDownList({
autoBind: false, // won’t try and read from the DataSource when it first loads
cascadeFrom: "categories", // the id of the DropDown you want to cascade from
optionLabel: "انتخاب محصول...",
dataTextField: "ProductName",
dataValueField: "ProductId",
dataSource: {
// When the serverFiltering is disabled, then the combobox will not make any additional requests to the server.
serverFiltering: true, // the DataSource will send filter values to the server
transport: {
read: {
url: "@Url.Action("GetProducts", "Home")",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET',
data: function () {
return { categoryId: $("#categories").val() };
}
}
}
}
});
});
</script>
<style scoped>
.demo-section {
width: 100%;
height: 100px;
}
</style>
}