【问题标题】:Json data not rendering in Kendo UI DropdownJson 数据未在 Kendo UI Dropdown 中呈现
【发布时间】:2014-03-11 18:40:07
【问题描述】:

尝试将Json 返回的数据渲染到kendo 下拉列表,但不渲染。请找到代码 sn-pt。我能够看到警报框。我试过JSON.parse(siteNameData),但没有运气。

AJAX 调用

$.ajax({
    url: '../Report/GetSitesofSelectedClient',
    type: "GET",
    cache: false,
    datatype: "json",
    data: { "selectedClientCode": selectedClientCode },
    contentType: "application/json",
    async: false,
    success: function(siteNameData) {
        alert('hello');

        $("#siteNamesDropDown").kendoDropDownList({
            dataTextField: "text",
            dataValueField: "value",
            template: $("#CheckboxTemplate2").html(),
            datasource: siteNameData,
            placeholder: "Select...",
            select: function(e) {
                e.preventDefault();
            }
        }).data("kendoDropDownList");

        //PopulateSiteNamesDropDown(siteNamesReceived);
     },
     error: function(xhr, ajaxOptions, thrownError) {
         ShowDialogError(xhr, 'High Chart Status Report');
     }
});

控制器

    public JsonResult GetSitesofSelectedClient(string selectedClientCode)
    {
        ViewBag.ShowReport = true;

        var highChartModel = new HighChartViewModel();

        var siteData = highChartModel.GetListOfSites(selectedClientCode);

        return Json(new {siteData}, JsonRequestBehavior.AllowGet);

    }

型号

 public string GetListOfSites(string clientCode)
 {

         SiteNameList = _serviceSessionManager.GetSiteListForClient(clientCode);

        listOfSiteNames = new List<SiteStatusReportModel>();

        foreach (Site siteName in SiteNameList)
        {
            var siteNameInfo = new SiteStatusReportModel
            {
                text     = siteName.SiteName,
                value    = siteName.SiteCode,
                selected = false
            };

            listOfSiteNames.Add(siteNameInfo);
        }

        var siteNameJsonData = JsonHelper.ToJsonString(listOfSiteNames)
        return siteNameJsonData;
 }

【问题讨论】:

    标签: jquery json asp.net-mvc-4 kendo-ui


    【解决方案1】:

    您不会每次都重新初始化下拉菜单。只初始化一次。在您的控制器上构建一个列表并通过 json 返回它。要重置下拉列表,您需要像这样设置数据源

    var combobox = $("#siteNamesDropDown").data('kendoDropDownList');
    if(combobox != null){
        //set the datasource of the combo
        combobox.dataSource.data(siteNameData);
        //requery the drop down so it shows the new data
        combobox.dataSource.query();
        //reset the selected value
        combobox.value("");
    }
    

    【讨论】:

    • 谢谢马特。 1)。每次 json 将数据返回到控制台时,我都需要附加。在这种情况下,我需要做什么。 2)。我需要从现有下拉列表中删除一些项目。 3)。如果数据源为空,我需要做什么。即,有时json数据是[]。
    • 此代码用于下拉菜单初始化后。我们在页面加载时这样做。设置数据源并执行上面的 .query 将清除旧数据。数据源是否为 null 无关紧要,因为您在此处设置它
    • 谢谢马特。初始化后,当尝试获取数据时,如果列表为空,我会收到错误消息。我怎样才能避免这种情况。
    • 如果您返回的是空的,那么我会在上面的代码周围放置一个 if(siteNameData != ""){ 这样它只会在其中有内容时触发。查看空时返回的内容并检查可能是“”,null,未定义
    • 谢谢马特。工作正常。正如您所说,每次返回 json 数据时,我都在初始化。拯救了我的一天。
    【解决方案2】:

    您是否使用了 console.log(siteNameData)?它是否返回正确的json?可能的原因是 inisde 变量 siteNameData.d 也不确定为什么将 async 属性设置为 false?请将其设置为 true 或者删除它。

    试试这个代码

    $.ajax(
                            {
                                url: '../Report/GetSitesofSelectedClient',
                                type: "GET",
                                cache: false,
                                datatype: "json",
                                data: { "selectedClientCode": selectedClientCode },
                                contentType: "application/json",
    
                                success: function(siteNameData) {
    
                                    alert('hello');
    
                                    $("#siteNamesDropDown").kendoDropDownList({
                                        dataTextField: "text",
                                        dataValueField: "value",
                                        template: $("#CheckboxTemplate2").html(),
                                        datasource: JSON.parse(siteNameData.d),
                                        placeholder: "Select...",
                                        select: function(e) {
                                            e.preventDefault();
                                        }
                                    }).data("kendoDropDownList");
    
                                    //PopulateSiteNamesDropDown(siteNamesReceived);
                                },
                                error: function(xhr, ajaxOptions, thrownError) {
    
                                    ShowDialogError(xhr, 'High Chart Status Report');
                                }
                            });
    

    【讨论】:

    • SyntaxError: JSON.parse: 意外字符 PlotHighChartReport:458 19:16:02.679 未定义。当我 console.log(siteNameData) 第一次渲染时,我得到下面的第一个直接从模型工作的另一个案例。第二个不工作1)。选择的对象:true text: "Halton East" value: "AG0028" (2) siteData: "[{selected: true, "text":"Halton East", "value":"AG0028"},
    【解决方案3】:

    这是我的下拉列表代码。确保您的 DataText 和 DataValue 字段正确映射到您的目标属性。

     public JsonResult GetOpportunityListByAccount(Guid Id)
        {
            List<OpportunityViewModel> cpvm = new List<OpportunityViewModel>();            
            cpvm = GetOpportunityListByAccount(Id);            
    
            return Json(cpvm, JsonRequestBehavior.AllowGet);            
        }   
    
    public List<OpportunityViewModel> GetOpportunityListByAccount(Guid Id)
        {
            List<OpportunityViewModel> oppVMList = new List<OpportunityViewModel>();
            var oppList = new OrderManager().GetOpportunitiesByAccount(Id); 
    
            foreach (var op in oppList)
            {
                OpportunityViewModel opvm = new OpportunityViewModel();
                opvm.OpportunityId = op.OpportunityId;
                opvm.OpportunityName = op.OpportunityName;
    
                oppVMList.Add(opvm);
            }
    
            return oppVMList;           
        }    
    
    
    @(Html.Kendo().DropDownListFor(x => x.FromOpportunity)       
              .Name("OpportunityDDL")                 
              .DataTextField("OpportunityName")              
              .DataValueField("OpportunityId")                         
              .DataSource(source => {
                  source.Read(read =>
                   {
                       read.Action("GetOpportunityListByAccount", "CrmIntegration")
                            .Data("OnAdditionalData");
                   })
                    . ServerFiltering(true);
              })     
         //    .CascadeFrom("AdvertiserDDL")
              .HtmlAttributes( new { style = "margin-left:13px; width: 275px;" })
       )     
    

    .Data 是您将用于将参数传递给控制器​​的内容。请注意,我的 GetOpportunityListByAccount 函数将 Id 作为参数。而我的“OnAdditionalData”也返回一个名为Id的参数。他们必须被称为相同的工作。

    function OnAdditionalData() {        
        return {
            Id: $("#AdvertiserDDL").val()
       };
    }      
    

    如果调用不起作用,请小心,这可能是数据类型问题。当我的控制器需要一个 Guid 时,我遇到了问题,使用 .val() 这也是一个 guid,但它没有工作。我不得不改变我的控制器来期待一个字符串,并做类似Id: "" + $("#AdvertiserDDL").val()

    【讨论】:

    • 我尝试了同样的方法。但没有在下拉列表中填充数据。我得到了一个可以在console.log 中看到的对象数组。 [对象,对象],[对象,对象},
    • 什么是.Data("OnAdditionalData"),我需要传入一个参数才能在控制器中运行。我该怎么做?
    • 谢谢。我也尝试了您的解决方案并且正在工作。但我在整个项目中采用了不同的方法,而 Matts 解决方案适合我的场景。感谢您的帮助,现在了解我们如何使用 kendo 扩展从视图中发送数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多