Could someone please help:
I am reviewing ASP MVC Core using the Telerik KendoUI Grid for our company and I'm having an issue doing the following. I am trying to use an html select to send, via an ajax call, the selected count value to the controller to reload the grid with the amount of database rows in the count. I default the grid count to 50 on load with the following in _Layout.cshtml.
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="LocalOptions" asp-action="KendoTest" asp-route-count="50">KendoTest</a>
</li>
What is happening, is the grid loads with the 50 rows. Then, when I select from the drop down ie: 500, the ajax function calls the controller action and the parameter count and ViewData["SelectedOption"] are set to 500. Then in the Views first @{ } code block, the database call fires and repopulates the list CurrOptionData with the new list of data. The problem is that the Kendo grid never reloads with the new rows, it stays at the initial 50.
Note: I have tried using $("#dataGrid").data("kendoGrid").dataSource.read(); and $("#dataGrid").data("kendoGrid").refresh(); in the ajax success: but no change.
Any help would be greatly appreciated.
Thanks
Dave.
********* LocalOptionsController.cs *********
$("#dataGrid").data("kendoGrid").refresh(); public IActionResult KendoTest(int count)
{
ViewData["SelectedOption"] = count;
return View();
}
************ VIEW ************
@model List<Asp_Core_MVC.Models.CurrOption>
@inject IERNIEData _db
<div class="text-center">
<h1 class="display-4">Current Options</h1>
</div>
<br />
<div>
<div class="text-center">
@{
List<CurrOption>? CurrOptionData;
int countToDisplay = 50;
int loadedRows = 50;
countToDisplay = Convert.ToInt32(ViewData["SelectedOption"]);
loadedRows = countToDisplay;
CurrOptionData = await _db.GetCurrOptionsByCount(countToDisplay);
}
@if (CurrOptionData == null)
{
<p><em>Loading ...</em></p>
}
else
{
<table>
<tr>
<td style="padding: 0 15px; text-align: left; vertical-align: middle;">Sample Row Count:</td>
<td style="text-align: left; vertical-align: middle;"><label id="lblCount">@loadedRows</label></td>
</tr>
</table>
<br />
<div>
<table>
<tr>
<td style="padding: 0 15px; text-align: center;">
Select Current Options To Display:
</td>
<td style="text-align: center;">
<select name="options" id="OptionCount" style="height: 25px;">
<option value="">--Please choose an option--</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="10000">10000</option>
<option value="100000">100000</option>
<option value="200000">200000</option>
<option value="300000">300000</option>
<option value="400000">400000</option>
</select>
</td>
</tr>
</table>
</div>
<br /><br />
@(
Html.Kendo().Grid(CurrOptionData)
.Name("dataGrid")
.Columns(columns =>
{
columns.Bound(p => p.ID).Title("ID").Width(105);
columns.Bound(p => p.USID).Title("USID").Width(120);
columns.Bound(p => p.SellID).Title("SellID").Width(130);
columns.Bound(p => p.OptionID).Title("OptionID").Width(170);
})
.Pageable()
.Sortable()
.Scrollable(scr => scr.Height(430))
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
)
)
}
</div>
</div>
<script>
document.getElementById('OptionCount').addEventListener('change', function () {
var selectedValue = this.value;
$.ajax({
url: '@Url.Action("KendoTest", "LocalOptions")',
type: 'GET',
data: { count: selectedValue },
success: function () {
$("#dataGrid").data("kendoGrid").dataSource.read();
$("#dataGrid").data("kendoGrid").refresh();
//alert(selectedValue);
},
error: function () {
console.log('Error occurred during the AJAX call.');
}
});
});
</script>