Jquery Json数据未在剑道UI下拉列表中呈现
尝试将Jquery Json数据未在剑道UI下拉列表中呈现,jquery,json,asp.net-mvc-4,kendo-ui,Jquery,Json,Asp.net Mvc 4,Kendo Ui,尝试将Json返回的数据呈现到kendo下拉列表,但不呈现。请查找代码片段。我可以看到警报框。我尝试了JSON.parse(siteNameData),但没有成功 AJAX调用 控制器 模型 公共字符串GetListOfSites(字符串clientCode) { SiteNameList=\u serviceSessionManager.GetSiteListForClient(客户端代码); listOfSiteNames=新列表(); foreach(站点名称列表中的站点名称) { var
Json
返回的数据呈现到kendo
下拉列表,但不呈现。请查找代码片段。我可以看到警报框。我尝试了JSON.parse(siteNameData)
,但没有成功
AJAX调用
控制器
模型
公共字符串GetListOfSites(字符串clientCode)
{
SiteNameList=\u serviceSessionManager.GetSiteListForClient(客户端代码);
listOfSiteNames=新列表();
foreach(站点名称列表中的站点名称)
{
var siteNameInfo=新站点状态报告模型
{
text=siteName.siteName,
value=siteName.SiteCode,
所选=错误
};
添加(siteNameInfo);
}
var siteNameJsonData=JsonHelper.ToJsonString(listOfSiteNames)
返回siteNameJsonData;
}
您是否有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');
}
});
您不会每次都重新初始化下拉列表。只初始化一次。在控制器上构建一个列表,并通过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("");
}
这是我的下拉列表代码。确保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;" })
)
如果调用不起作用,请小心,这可能是数据类型问题。当我的控制器需要Guid时,我遇到了这个问题,使用了.val(),它也是一个Guid,但不起作用。我必须更改我的控制器以期望字符串,并执行类似于
Id:+$(“#广告商ddl”).val()的操作SyntaxError:JSON.parse:意外字符PlotHighChartReport:458 19:16:02.679未定义。当我使用console.log(siteNameData)时,我会发现第一个在第一次渲染时直接从模型中处理另一个案例。第二个不起作用(1)。所选对象:真文本:“Halton East”值:“AG0028”(2)站点数据:“[{所选:真,“文本”:“Halton East”,“值”:“AG0028”}”,谢谢Matt.1)。每次我都需要将json返回的数据附加到控制台。在这种情况下,我需要做的是。2)。我需要从现有下拉列表中删除一些项。3)。如果数据源为空,我需要做的是。即,有时json数据为[]。此代码是在下拉列表初始化后使用的。我们在页面加载时执行此操作。设置数据源并执行上面的.query将清除旧数据。数据源是否为空并不重要,因为您在此处进行了设置。谢谢Matt。初始化后,当尝试获取数据时,如果列表为空,我将得到错误。如何避免。如果返回的内容为空,则我会在上面的代码周围放置一个if(siteNameData!=“”){,这样只有在其中有内容时才会触发。请查看空时返回的内容,并检查可能为空的内容”“,null,UndefinedHanks Matt。工作正常。正如您所说,每次返回json数据时,我都在初始化。节省了时间。我尝试了相同的方法。但没有在下拉列表中填充数据。我得到了一个对象数组,可以在console.log中看到。[object,object],[object,object},what is.data”(“OnAdditionalData”),我需要将一个参数传递到控制器中的函数中。我如何才能做到?谢谢。我也尝试了您的解决方案并正在工作。但我在整个项目中采用了不同的方法,Matts解决方案适合我的方案。感谢您的帮助,现在已经了解了如何使用剑道扩展从视图发送数据。
$.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');
}
});
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("");
}
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;" })
)
function OnAdditionalData() {
return {
Id: $("#AdvertiserDDL").val()
};
}