我正在通过ajax调用来调用控制器,以将数据绑定到kendo网格。当我与[DataSourceRequest] DataSourceRequest请求一起发送附加参数时,网格不会绑定数据。第二个参数是将值正确获取到控制器,并从数据库中获取正确的json数据。但是只有绑定才是问题。
我尝试将JsonRequestBehavior.AllowGet添加到我的Json返回中。
home.cshtml
在此文件中,对于“呼叫记录”字段,我正在发送2个参数。即([DataSourceRequest]DataSourceRequest
请求和CallRecording,如下面的代码所示。
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Kendo UI!</title>
<!-- jQuery JavaScript -->
<script src="~/Kendo/js/jquery.min.js"></script>
<!-- Common Kendo UI CSS for web widgets and widgets for data visualization. -->
<link href="~/Kendo/styles/kendo.common.min.css" rel="stylesheet" />
<!-- (Optional) RTL CSS for Kendo UI widgets for the web. Include only in right-to-left applications. -->
<link href="~/Kendo/styles/kendo.rtl.min.css" rel="stylesheet" type="text/css" />
<!-- Default Kendo UI theme CSS for web widgets and widgets for data visualization. -->
<link href="~/Kendo/styles/kendo.default.min.css" rel="stylesheet" />
<!-- (Optional) Kendo UI Hybrid CSS. Include only if you will use the mobile devices features. -->
<link href="~/Kendo/styles/kendo.default.mobile.min.css" rel="stylesheet" type="text/css" />
<!-- Kendo UI combined JavaScript -->
<script src="~/Kendo/js/kendo.all.min.js"></script>
@Scripts.Render("~/bundles/jquery")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" > </script>
</head>
<body>
<div id="recordingsDetails" class="col-md-8" style="padding-top: 37px;">
@(Html.Kendo().Grid<MiCustomerSentiment.SentimentAnalytics>()
.Name("home")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("GetRecordingDetails", "Home"))
)
.Columns(columns =>
{
//columns.Bound(user => user.CallRecording).Width(300).ClientTemplate("<a href='" + Url.Action("Audio", "Home") + "?callRecordingId=#= CallRecording #'" + ")'>#= CallRecording #</a>");
//columns.Bound(user => user.CallRecording).Width(300).ClientTemplate(Html.ActionLink("#= CallRecording #", "Audio", "Home", new { callRecordingId = "<#= CallRecording #>" }).ToString()).Title("Call Recordings");
columns.Bound(user => user.CallRecording).Width(300).Title("Call Recordings").ClientTemplate(Ajax.ActionLink("#= CallRecording #", "Audio", "Home", new { callRecordingId = "#= CallRecording #" }, new AjaxOptions() { OnSuccess = "myJSFunction" }).ToHtmlString());
columns.Bound(user => user.AgentName).Title("Agent Name").ClientTemplate(Ajax.ActionLink("#= AgentName #", "Agent", "Home", new { CallRecording = "#= CallRecording #" }, new AjaxOptions() { OnSuccess = "myJSFunctionForAgentName" }).ToHtmlString());
columns.Bound(user => user.CustomerType).Title("Customer Type");
columns.Bound(user => user.CaseNumber).Title("Case Number");
})
.HtmlAttributes(new { style = "height: 312px;" })
.Scrollable(a => a.Height("auto"))
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5)))
</div>
@if (@ViewBag.DoNotLoadAudioPartialView = true)
{
<div id="audio" class="col-md-4" style="padding-top: 37px;">
@Html.Partial("Audio")
</div>
}
<div id="agentHome" class="col-md-6" style="padding-top: 37px; width: 1237px;">
<h3>Call Summary</h3>
@Html.Partial("Agent")
</div>
<div id="caseDetailsHome" class="col-md-6" style="padding-top: 37px; width: 1237px;">
<h3>Case Summary</h3>
@Html.Partial("CaseDetails")
</div>
</body>
</html>
HomeController.cs
这是后端控制器方法,它接受2个参数。当从视图发送到callrecording参数的值时,如果使用相同的参数值,则网格未绑定数据。如果我直接对callRecording的值进行硬编码,那么网格将绑定数据。在这两种情况下,都将返回预期的json值。
[HttpGet]
public ActionResult Agent([DataSourceRequest]DataSourceRequest request, string CallRecording)
{
using (var data = new SampleData())
{
IQueryable<SentimentAnalytics> semanticAnalyticsofUsers = data.SentimentAnalytics.Where(a => a.CallRecording.Equals(CallRecording));
DataSourceResult result = semanticAnalyticsofUsers.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
}
局部视图(Agent.cshtml)
@using Kendo.Mvc.UI;
@(Html.Kendo().Grid<MiCustomerSentiment.SentimentAnalytics>()
.Name("agent")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("Agent", "Home", new { CallRecording = "#= CallRecording #" }))
)
.Columns(columns =>
{
columns.Bound(user => user.AgentName).Title("Agent Name");
columns.Bound(user => user.CustomerType).Title("Customer Type");
columns.Bound(user => user.CaseNumber).Title("Case Number").ClientTemplate(Ajax.ActionLink("#= CaseNumber #", "CaseDetails", "Home", new { callRecordingId = "#= CallRecording #" }, new AjaxOptions() { OnSuccess = "myJSFunctionForCaseDetails" }).ToHtmlString());
columns.Bound(user => user.InteractionTime).Title("Talk Time");
columns.Bound(user => user.IdleTime).Title("Idle Time");
//columns.Bound(user => user.Emotion).Title("Emotion").ClientTemplate("#= Emotion # <img src='../../Images/positive_smiley.jpg' style='width:40px;'/>");
//columns.Bound(user => user.Emotion).Title("Emotion").ClientTemplate("#= Emotion # #=GetImage(user.Emotion)# ");
columns.Bound(user => user.Emotion).Title("Emotion").ClientTemplate("#= Emotion # #=GetImage(data.Emotion)#");
})
.HtmlAttributes(new { style = "height: 130px;" })
.Scrollable(a => a.Height("auto"))
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5)))
请告诉我,为什么使用参数的实际值时网格未绑定数据?
在控制器中使用传递给控制器的其他参数时,Kendo网格应绑定数据。