gpt4 book ai didi

asp.net-mvc - 使用 Ajax 在 ASP.NET MVC 的 Kendo UI 中级联 DropDownList?

转载 作者:行者123 更新时间:2023-12-03 18:58:54 25 4
gpt4 key购买 nike

有人可以使用 ajax 在 ASP.NET MVC 的 Kendo UI 中提供级联下拉列表的示例吗?我说的是 Helper 方法 ( @Html.Kendo().DropDownList() ) 我知道子下拉列表必须调用 CascadeFrom("ParentDropDownListName")但是 Controller Action 是什么样的呢?当我尝试将它们连接起来时,我得到了传递给子下拉列表操作方法的参数的空异常。我假设在幕后,Kendo 正在提取父选择的 DataValueField 并将其附加到 Controller 操作请求到子下拉列表,但似乎没有发生这种情况。

更新:我相信这与发送到我的 Controller 操作的“过滤器”集合有关。我只是不知道如何在我的 Controller 操作中处理传入的过滤器集合/对象。

最佳答案

我想您已经阅读了 cascading dropdownlist 的示例。在带有 cshtml 代码源的 Kendo UI 站点上。

第二个下拉列表对应于产品,并按以下方式从类别级联:

    @(Html.Kendo().DropDownList()
.Name("products")
.OptionLabel("Select product...")
.DataTextField("ProductName")
.DataValueField("ProductID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetCascadeProducts", "ComboBox")
.Data("filterProducts")
.Type(HttpVerbs.Post); // This line serves to accept POST requests
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("categories")

(请注意,我必须添加行以接受 Post 请求)

在此示例中,您将需要一个具有以下语法的 Controller :
    [HttpPost]
public JsonResult GetCascadeProducts(int category)
{
List<Product> Products = new List<Product>();

Products.Add(new Product(1, 0, "Chai"));
Products.Add(new Product(1, 1, "Chang"));
Products.Add(new Product(1, 2, "Guarana Fantastica"));
Products.Add(new Product(2, 0, "Aniseed Syrup"));
Products.Add(new Product(2, 1, "Seasoning"));

var ProductsInCategory = from p in Products where p.CategoryID == category select p;

return Json(ProductsInCategory);
}

我的类(class)模板如下:
    public class Product
{
public int CategoryID { get; set; }
public int ProductID { get; set; }
public string ProductName { get; set; }

public Product(int category, int id, string name)
{
CategoryID = category;
ProductID = id;
ProductName = name;
}
}

如果您有正确的 Javascript 功能:
function filterProducts() {
return {
category: $("#categories").val()
};
}

(类别应与 Controller 方法中的参数名称相同)
正常情况下,一切正常!

关于asp.net-mvc - 使用 Ajax 在 ASP.NET MVC 的 Kendo UI 中级联 DropDownList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com