New to Kendo & MVC, so please bear with me.
I have a view that contains a grid that is working almost perfectly for me. My model is a System.Data.DataTable which is generated by a service, and is entirely dynamic (in that I don't know the schema until runtime). I'm injecting the model directly into the View, which uses the DataColumn definitions in the DataTable to generate the presentation layer (GridColumnSettings):
@using System.Data
@using System.Linq
@using Kendo.Mvc.UI
@model System.Data.DataTable
@(Html.Kendo()
.Grid(Model)
.Name("View")
.Columns(columns => columns.LoadSettings(
Model.Columns.Cast<
DataColumn
>().Select(dc => new GridColumnSettings {
Member = dc.ColumnName,
Title = dc.Caption,
MemberType = dc.DataType,
Format = "{0:" + dc.ExtendedProperties["format"] + "}",
Width = "100px"
})))
.Sortable()
.Scrollable()
)
I'm quite happy with this approach - it's simple and elegant and keeps the Model (arguably ViewModel) and Controller blissfully ignorant of any Kendo/UI concepts. The only problem with this is that sorting always calls back into the Controller, and the DataTable is regenerated, even though it will not have changed.
I've seen in some Ajax samples the use of a 'serverSorting' property on a 'dataSource' instance, and I think I need to set that to false in order to get what I want, but I can't see how that applies in my MVC scenario. I don't want to set up a DataSource() that makes an Ajax() call that then Read()s the returned Json, because I have all the data already, but I'm struggling to see how else would I get access to the DataSource.ServerSorting property.
What am I missing?
.IncludeInMenu(false)
<
tr
>
<
td
>
</
td
>
<
td>
</
td
>
</
tr
>
.HtmlAttributes(new { id = Guid.NewGuid().ToString() })
$("input[name=ThicknessDry]").val();
<
section
id
=
"maincontent"
>
<
div
class
=
"container"
>
@(Html.Kendo().Grid<
Person
>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(s => s.PersonID);
columns.Bound(s => s.FirstName);
columns.Bound(s => s.Surname);
columns.Bound(s => s.IsEmployed);
columns.Command(c =>
{
c.Edit();
c.Destroy();
});
})
.ToolBar(tools =>
{
tools.Create();
})
.Sortable()
.Pageable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(s => s.PersonID);
})
.Read(read => read.Url("api/Person").Type(HttpVerbs.Get))
.Create(create => create.Url("api/Person").Type(HttpVerbs.Post))
.Update(update => update.Url("api/Person").Type(HttpVerbs.Put))
.Destroy(destroy => destroy.Url("api/Person").Type(HttpVerbs.Delete))
)
)
</
div
>
</
section
>
<
script
>
$(function() {
var grid = $("#Grid").data("kendoGrid");
// WebAPI needs the ID of the entity to be part of the URL e.g. PUT /api/Person/PutPerson/80
grid.dataSource.transport.options.update.url = function(data) {
return "api/Person/PutPerson/" + data.personID;
};
// WebAPI needs the ID of the entity to be part of the URL e.g. DELETE /api/Person/DeletePerson/80
grid.dataSource.transport.options.destroy.url = function(data) {
return "api/Person/DeletePerson/" + data.personID;
};
});
</
script
>
namespace
KiScope.RemoteClient.WebAPI.Controllers.Apis
{
// Specify on Controller level that the user has to authorize before he can start
// The actual implementation of the Redirect to Login functionality has to be done on the client side
// here is a video explaining this: http://www.asp.net/web-api/videos/getting-started/authorization
//[Authorize]
public
class
PersonController : ApiController
{
// Remember that the UOW disposes itself after usage, so no need to handle this manually
private
readonly
IRemoteClientUoW uow;
// Ninject Initialization
public
PersonController(IRemoteClientUoW _uow)
{
uow = _uow;
}
public
DataSourceResult GetPeople([ModelBinder(
typeof
(ModelBinders.DataSourceRequestModelBinder))] DataSourceRequest request)
{
var seaman = uow.Person.GetAll();
return
seaman.ToDataSourceResult(request,s =>
new
{
s.PersonID,
s.FirstName,
s.Surname,
s.IsEmployed
}
);
}
// ... MORE CODE
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:D}")]
[Display(Name = "Visit Date")]
public DateTime VisitDate { get; set; }
[17:39:18.580] VisitDate: Fri May 10 2013 00:00:00 GMT-0400 (Eastern Daylight Time)
function deleteRow() {
var grid = $('#scheduleGrid').data('kendoGrid');
var rows = grid.select();
if (rows.length > 0) {
var dataItem = grid.dataItem(rows);
console.log("VisitDate: " + dataItem.VisitDate);
}
}