I'm trying to recreate a data grid that I've got working with the old MVC extensions, with the Kendo UI MVC extensions, but the documentation is virtually non-existent. Looking at the example, I've managed to get data into a grid, but it doesn't sort or filter (I'm not sure about apging, as there's only a few records at present.
The view is:-
@(Html.Kendo().Grid<
PLCV2.Models.PLCVUser
>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.UserName);
columns.Bound(p => p.FullName);
columns.Bound(p => p.SystemAdmin);
columns.Bound(p => p.SystemUser);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("AjaxBinding", "UserMgmt"))
)
)
and the controller is:-
public
ActionResult AjaxBinding([DataSourceRequest] DataSourceRequest request)
{
Models.PLCVdBDataContext db =
new
Models.PLCVdBDataContext();
var query = from p
in
db.PLCVUsers
select p;
//query = query.OrderByDescending(c => c.UserName);
return
Json(query.ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
}
How do I enable sorting and filtering?
Also, I had to add the AllowGet JsonRequestBehaviour, to get the grid to work. Your examples don't seem to require this - how?
Where is the documentation for the MVC extensions? I can only find a very brief (and very lacking!) migration overview, which fails to mention many of the server-side changes (no GridAction, the new DataSourceRequest etc..).
I'm keen to try out the new extensions, but without more documentation, it doesn't seem feasible.
18 Answers, 1 is accepted
The documentation can be found here: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/introduction
Your code looks correct. Could you please clarify what do you mean by "sorting and filtering does not work"? Do you see a JavaScript error? What happens when you click on a column header? Did you check the developer console of your browser?
Atanas Korchev
the Telerik team
In fiddler, the request being fired is:- http://localhost:50793/UserMgmt/AjaxBinding?take=10&skip=0&page=1&pageSize=10&sort%5B0%5D%5Bfield%5D=UserName&sort%5B0%5D%5Bdir%5D=asc but no sort is taking place.
Again, filtering behaves the same way, the filter options are displayed, an ajax request is fired, but the data remains the same. An error seems to be generated when the filter (which isn't applied) is cleared, it generates the 'Object reference not set to an instance of an object.' error.
However, paging does seem to work.
The link you have posted is not to documentation, but just a brief guide, which hardlytouches the surface - it doesn't give details of the server side changes at all - a fairly major omission!
The documentation of the Kendo Grid for ASP.NET MVC can be found here: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/overview
I am afraid I cannot reproduce the behavior you are seeing. I have created a sample project for you which shows a working implementation. What is different in your case?
Atanas Korchev
the Telerik team
I finally found the cause - I had referenced the DataViz scripts as well.
<
link
href
=
"@Url.Content("
~/Content/kendo.dataviz.min.css")"
rel
=
"stylesheet"
type
=
"text/css"
/>
<
script
src
=
"@Url.Content("
~/Scripts/kendo.dataviz.min.js")"></
script
>
After removing these, the grid functioned as expected.
Aren't the UI and data Viz components compatible?
Thanks for the documentation link - I'll bookmark it!
The Web and DataViz components are compatible. You however need to include kendo.all.min.js instead of kendo.web.min.js and kendo.dataviz.min.js if you want to use both. We will update the documentation to reflect that.
Regards,Atanas Korchev
the Telerik team
I observe the similar problem:
Here is View part
@(Html.Kendo().Grid<
PersonToken
>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(o => o.FirstName);
columns.Bound(o => o.LastName);
columns.Bound(o => o.IsActive);
})
.Pageable()
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource.Ajax()
.Model(model => model.Id(p => p.PersonId))
.Read(read => read.Action("ReadPersons", "Person"))
)
)
Here controller
public ActionResult ReadPersons([DataSourceRequest] DataSourceRequest request)
{
var list = PersonLibRepository.All();
return Json(list.ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
}
In debug mode I see what Sorts and Filters equals NULL , whenever in firebug on client side I see all requested parameters(filters/sorts) correctly. Please see attached screen shots.
The same problem I receive also on Server binding like :
@model IEnumerable<
PersonToken
>
@(Html.Kendo().Grid(Model)
.Name("person-report-grid")
.Columns(columns => {
columns.Bound(p => p.FirstName);
columns.Bound(p => p.LastName);
columns.Bound(p => p.IsActive);
columns.Bound(p => p.Contact.Email);
columns.Bound(p => p.Contact.Phone);
columns.Bound(p => p.Contact.CellPhone);
columns.Template(
@<
text
>
@Html.ActionLink("Edit", "PersonManage", new BaseToken {name = String.Format("{0} {1}",@item.FirstName,@item.LastName), id = @item.PersonId }, new { @class = "k-button" })
</
text
>
);
})
.ToolBar(toolbar => toolbar.Custom().Action("PersonManage", "Person", new BaseToken{ name = string.Empty,id = Guid.NewGuid() }).Text("Add new person"))
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource.Server().Model(model => model.Id(p => p.PersonId)))
)
public ActionResult Persons([DataSourceRequest] DataSourceRequest request)
{
var list = PersonLibRepository.All();
return View(list);
}
Where I'm wrong ?
Please help
Best Regards
Serge
Can you confirm that you have included kendo.aspnetmvc.min.js as outlined in the documentation?
All the best,Atanas Korchev
the Telerik team
I very confused , but i forgot to add it. Now It's work.
On the way:
I prefer to use Ajax binding. But I need OnCreate/Update redirect it to other page.
I read , what column template works only on Server binding.
Exists , some other ways to solve a problem (Server bind grid from my code example)
Thanks again and nice day
Serge
P.S. I solved it in following way :
columns.Bound(m => m.PersonId).ClientTemplate("<
a
href
=
'Redirect2PersonManage/#=PersonId#?name=#=FirstName + '
' + LastName#'
class
=
'k-button'
>Edit</
a
>").Title("");
I had the same problem as a few other people. The DataSourceRequest object was coming through with a NULL Sorts collection and the AJAX request was a GET instead of a POST.
The culprit for me was the order of my script includes. You MUST include the kendo.web.min.js file BEFORE the kendo.aspnetmvc.min.js for it to work correctly. Otherwise you will get a javascript error on page load saying "TypeError c is undefined" and the error stating that you must set the JsonRequestBehavior to AllowGet.
I was using the new BundleConfig in MVC 4 and simply adding a script include for "~/Scripts/kendo*". The * was adding the scripts in alphabetic order, which in this scenario was wrong. Just something else to watch out for with MVC 4.
Scott McNeany
No matter what i do i can't get the Ajax updating to work properly. I have this error in my console
Uncaught TypeError: Object [object Object] has no method 'kendoGrid'
my bundle set up looks like this:
bundles.Add(new ScriptBundle("~/bundles/kendo").Include("~/Scripts/Kendo/jquery.min.js",
"~/Scripts/Kendo/kendo.web.min.js",
"~/Scripts/Kendo/kendo.aspnetmvc.min.js"
));
The gird displays, but does not load any data, but is styled correctly.
if i use it without ajax i still get this uncaught type error, but i can sort and load data just fine.
You probably have jQuery included twice.
All the best,Atanas Korchev
the Telerik team
Remove this line:
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
And the ajax will start working again :)
I am using ASP.Net MVC 4 with Razor, and have used NuGet to update all standard packages that are included in a new MVC 4 project (this includes jQuery). I used NuGet to install KendoUI.
The data loads into the view just fine. But whenever I try a sort or filter, the results are unchanged. I would *like* for all of this to be done client-side (I get the data for the list when the page is loaded and would prefer not going back to the database at all). But if I need to go round-trips on these things, it's fine. I can keep the data in the controller if it needs to go back there.
I have already checked for multiple jquery refs -- don't have them. I have also checked for the aspnetmvc js file in the kendo folder -- it doesn't exist. I'm guessing it's been deprecated?
Here is the code in my _layout.cshtml to include the necessary scripts (I'm not including any twice):
<
script
src
=
"@Url.Content("
~/Scripts/jquery-1.8.3.min.js")"
type
=
"text/javascript"
></
script
>
<
script
src
=
"@Url.Content("
~/Scripts/modernizr-2.6.2.js")"
type
=
"text/javascript"
></
script
>
<
script
src
=
"@Url.Content("
~/Scripts/jquery.unobtrusive-ajax.min.js")"
type
=
"text/javascript"
></
script
>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<!-- BEGIN Telerik Kendo UI stuff -->
<
link
href
=
"~/Content/kendo/2012.3.1114/kendo.common.min.css"
rel
=
"stylesheet"
type
=
"text/css"
/>
<
link
href
=
"~/Content/kendo/2012.3.1114/kendo.default.min.css"
rel
=
"stylesheet"
type
=
"text/css"
/>
<
script
src
=
"~/Scripts/kendo/2012.3.1114/kendo.web.min.js"
type
=
"text/javascript"
></
script
>
<!-- END Telerik Kendo UI stuff -->
@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.MigrationId).Groupable(false);
columns.Bound(p => p.DestCd);
columns.Bound(p => p.PropertyNo);
columns.Bound(p => p.SupplierId);
columns.Bound(p => p.UserNm);
columns.Bound(p => p.Status);
})
.Groupable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("History", "Grid"))
)
)
public
ActionResult History()
{
//var migList = GetHistoryList();
var beginDt = DateTime.Now.AddDays(-10);
return
View(
new
dbSdkEntities(Helper.SdkConnection).PropertyMigrations.Where(a => a.BeginDs > beginDt));
}
Your action method is hot properly configured for ajax binding. Please refer to the ajax binding help article.
Greetings,Atanas Korchev
the Telerik team
Thanks again!
public
ActionResult History()
{
return
View();
// don't need to get data here -- we do that with the Json GetHistory method below.
}
public
ActionResult GetHistory([DataSourceRequest]DataSourceRequest request)
{
var beginDt = DateTime.Now.AddDays(-10);
var migList =
new
dbSdkEntities(Helper.SdkConnection).PropertyMigrations.Where(a => a.BeginDs > beginDt);
var result = migList.ToDataSourceResult(request);
return
Json(result, JsonRequestBehavior.AllowGet);
}