We are creating an MVC 4 application using the Kendo.Mvc helper assembly. The code is working well; however, we appear to be missing something when we try to obtain a reference to a kendo control.
Example:
Our application has an ActionLink on a toolbar. When the user clicks on the ActionLink, we call into a JS function to validate if the displayed grid has a row selected. Within the function we attempt to obtain a reference to the grid and get the selected row. Unfortunately, the select() function throws an error ("Value is undefined").
I have included the code below. Most of our logic will be based on controls having the ability to communicate with each other from events, etc. Are we able to receive the functionality using the MVC assembly, or are we only able to use the API when code in JS?
Any comments or assistance would be much appreciated.
Thanks,
Todd
@(Html.Kendo().Grid(Model.Transaction)
.Name("grid")
.Columns(columns => {
columns.Bound(o => o.EntityId)
.Hidden().Visible(false);
columns.Bound(o => o.EntityName)
.Sortable(true)
.Title("Entity Name")
.ClientTemplate(@Html.ActionLink("#=EntityName#", "Details", "Entity", new { id = "entityId" }, null).ToHtmlString().Replace("entityId", "#=EntityId#"));
columns.Bound(o => o.TransactionType);
columns.Bound(o => o.Name)
.ClientTemplate(@Html.ActionLink("#=Name#", "Details", "Engagement", new { id = "engId" }, null).ToHtmlString().Replace("engId", "#=ID#"));
columns.Bound(o => o.TransactionStatus);
columns.Bound(o => o.Initiator);
columns.Bound(o => o.Reason);
})
.Pageable(p => p.PageSizes(true))
.Filterable(filterable => filterable
.Extra(false)
.Operators(operators => operators
.ForString(str => str.Clear()
.StartsWith("Starts with")
.IsEqualTo("Is equal to")
.IsNotEqualTo("Is not equal to")
))
)
.Navigatable()
.Resizable(resize => resize.Columns(true))
.Selectable()
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.ID))
.Read(r => r.Action("Search", "Transaction")))
)
getSelectedRowId:
function
(gridId) {
var
grid = jQuery(gridId).kendoGrid().data(
"kendoGrid"
);
var
row = grid.select();
if
(row.length > 0)
return
grid.dataItem(row).id;
else
return
null
;
}
I tried this on http://jsfiddle.net/rusev/E3vYH/ and it works fine. Now I have a listView generated by ListView Kendo Helper and I add a model by calling DataSource.add() method, but when I call DataSource.sync() it doesn't call create and I don't see any http activity on my browser's network tab. I copied the code generated by the kendo helper and pasted it into a test html file and it doesn't work neither. It did add the model to the listView but never calls create. Here is my test file:
<!DOCTYPE html>
<html>
<head>
<title>Testing Kendoui dataSource</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
<script type="text/x-kendo-tmpl" id="template">
<div data-id = ${CountryCode}>
<p>${CountryName}</p>
</div >
</script>
<script>
$(function () {
$("#target").kendoListView ({
"dataSource": {
"transport": {
"prefix": "",
"read": function () {
alert("reading...");
},
"create": function () {
alert("creating...");
},
"destroy": function () {
alert("destroying...");
},
"update": function () {
alert("updating...");
}
},
"type": "aspnetmvc-ajax",
"schema": {
"data": "Data",
"total": "Total",
"errors": "Errors",
"model": {
"id": "SubaccountId",
"fields": {
"SubaccountId": {
"type": "number"
},
"CountryCode": {
"type": "string"
},
"CountryName": {
"type": "string"
}
}
}
}
},
"template": kendo.template($('#template').html()),
"selectable": "single"
});
$('#addButton').bind("click", function () {
var ds = $('#target').data('kendoListView').dataSource;
ds.add({
SubaccountId: 1,
CountryCode: 'US',
CountryName: 'United States'
});
ds.sync();
});
});
</script>
</head>
<body>
<div>
<button id="addButton">Add Country</button>
</div>
<div id="target"></div>
</body>
</html>
I tried with different jquery and kendo version too. No luck.
How do I make this work.
Thanks
D
@(Html.Kendo().NumericTextBoxFor(m => m.FilterParticipants).Format("n0").Min(1).Decimals(0))
[Required(ErrorMessageResourceName =
"ValidationRequired"
, ErrorMessageResourceType =
typeof
(Resources.Global))]
//[Min(1, ErrorMessageResourceName = "ValidationRequired", ErrorMessageResourceType = typeof(Resources.Global))]
public
int
FilterParticipants {
get
;
set
; }
columns.Bound(i => i.WeightUOM)
.EditorTemplateName("ItemGrid_RefUOMListingWeight")
.Width(50);
columns.Bound(i => i.NMFC).Width(50);
var itemGrid = $("#QuoteItemGrid").data("kendoGrid");
itemGrid.hideColumn("NMFC");
@(Html.Kendo().DatePicker()
.Name(
"DateForReport"
)
.Value(DateTime.Now)
.Events(e =>
{
e.Change(
"getStockInfo"
);
})
)
@(Html.Kendo().Grid<Supermarket.Main.Areas.Management.Models.ProductInStockViewModel>()
.Name(
"reportsByDateGrid"
)
.Columns(columns =>
{
columns.Bound(m => m.ProductName);
columns.Bound(m => m.CategoryName);
columns.Bound(m => m.Amount);
columns.Bound(m => m.PricePerUnit);
columns.Bound(m => m.TotalPrice);
})
.Pageable()
.Sortable()
)
function getStockInfo() {
if
(
this
.value() !=
null
) {
var date = kendo.toString(
this
.value(),
'd'
);
$.ajax({
type:
"get"
,
dataType:
"json"
,
url:
"@Url.Action("
GetAvailabilitiesByDate
")"
,
data: { date: date },
traditional:
true
,
success: function (result) {
if
(result.success) {
var data = result.data;
var newDataSource =
new
kendo.data.DataSource({
data: data,
pageSize: 15,
schema: {
model: {
fields: {
ProductName: { type:
"string"
},
CategoryName: { type:
"string"
},
Amount: { type:
"number"
},
PricePerUnit: { type:
"number"
},
TotalPrice: { type:
"number"
},
}
}
}
});
var grid = $(
"#reportsByDateGrid"
).data(
"kendoGrid"
);
grid.setDataSource(newDataSource);
grid.refresh();
}
else
{
alert(result.error);
}
},
error: function () {
alert(
"An error has occurred, please try again or inform an administrator!"
);
}
});
}
}
{
"success"
:
true
,
"data"
:[{
"ProductName"
:
"Borovec"
,
"CategoryName"
:
"Food"
,
"Amount"
:18,
"PricePerUnit"
:1.50,
"TotalPrice"
:27.00},{
"ProductName"
:
"Coca-cola"
,
"CategoryName"
:
"Beverages"
,
"Amount"
:25,
"PricePerUnit"
:2.50,
"TotalPrice"
:62.50},{
"ProductName"
:
"Medenka Lubimka"
,
"CategoryName"
:
"Food"
,
"Amount"
:23,
"PricePerUnit"
:1.50,
"TotalPrice"
:34.50}]}