【问题标题】:How to read Kendo Dropdownlist from controller action in Jquery如何从 Jquery 中的控制器操作中读取 Kendo Dropdownlist
【发布时间】:2015-01-22 06:07:29
【问题描述】:

我尝试从 Jquery 的控制器操作中读取 Kendo Dropdownlist。代码是

$("#CountryName").kendoDropDownList({
    dataTextField: "Description",
    dataValueField: "Id",
    dataSource: {
        transport: {
            read: {
                dataType: "jsonp",
                url: "Shared/CountryAjax"
            }
        }
    }
});

[HttpPost]
public ActionResult CountryAjax(string countryId)
{
    var countries = this._decodeBL.GetAllCountriesList();

    return new JsonResult
    {
        Data = new SelectList(countries, "Id", "Description", "Canada")
    };
}

但它从不执行 CountryAjax。我需要这方面的帮助。谢谢。

【问题讨论】:

    标签: jquery kendo-ui kendo-dropdown


    【解决方案1】:

    我从您的代码中发现了两个错误。首先,您应该在服务器方法中使用 [HttpGet] 注释。其次,您不应该在服务器方法中接收 countryId。

    请参阅下面的固定代码。

    $("#CountryName").kendoDropDownList({
        dataTextField: "Description",
        dataValueField: "Id",
        dataSource: {
            transport: {
                read: {
                    dataType: "jsonp",
                    url: "Shared/CountryAjax"
                }
            }
        }
    });
    
    
    [HttpGet]
    public ActionResult CountryAjax()
    {
        var countries = this._decodeBL.GetAllCountriesList();
    
        return new JsonResult
        {
            Data = new SelectList(countries, "Id", "Description", "Canada")
        };
    }
    

    【讨论】:

    • 抱歉,还是不行。 DropDownList 甚至没有显示出来。网址对吗?我已经看到使用了 url.content。我怀疑网址可能是错误的。
    • 需要用jsonp代替json吗?如果您使用的是 jsonp,则需要为其配置服务器,并且 url 必须指向该服务器。
    • 我怀疑你的网址有误。我建议您更改 jsonp -> json 并使用 Chrome 开发人员工具或 Fiddler 检查您的 http 流量。
    • jsonp或者json没有区别,我测试过了。
    【解决方案2】:
     $("#selectYears").kendoDropDownList({
            dataTextField: "Text",
            dataValueField: "Value",
            //dataSource: data2,
            dataSource: {
                transport: {
                    read: {
                        type: "GET",
                        dataType: "json",
                        url: '@Url.Action("GetYears", "NewHome")'
                    }
                }
            },
            index: 0,
            change: onChange
        });
    
       return new JsonResult
            {
                Data = new SelectList(years, "newHomeIndexid", "selectedYear"),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
    

    【讨论】:

    • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多