Hi,
Please can someone help as I cannot get the grid to display any results?
I have adapted the code from the grid example in the installed demos but cant get it to work. Please see my code below.
View:
Controller:
The grid is displayed and the web api controller GET is being called and is returning the DataSourceResult and no rows are displayed.
If anyone can help I would be grateful. Thanks in advance.
Please can someone help as I cannot get the grid to display any results?
I have adapted the code from the grid example in the installed demos but cant get it to work. Please see my code below.
View:
@(Html.Kendo().Grid<
StatusPage.Models.Appliance
>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Title);
columns.Bound(p => p.CurrentStatus).Width(100);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
})
.ToolBar(tools =>
{
tools.Create();
})
.Sortable()
.Pageable()
.Filterable()
.DataSource(dataSource =>
dataSource
.WebApi()
.Model(model =>
{
model.Id(p => p.Id);
})
.Events(events => events.Error("error_handler"))
.Read(read => read.Url(Url.HttpRouteUrl("Appliance", new { controller = "Appliances" })))
.Create(create => create.Url(Url.HttpRouteUrl("Appliance", new { controller = "Appliances" })))
.Update(update => update.Url(Url.HttpRouteUrl("Appliance", new { controller = "Appliances", id = "{0}" })))
.Destroy(destroy => destroy.Url(Url.HttpRouteUrl("Appliance", new { controller = "Appliances", id = "{0}" })))
)
)
<
script
>
function error_handler(e) {
var errors = $.parseJSON(e.xhr.responseText);
if (errors) {
alert("Errors:\n" + errors.join("\n"));
}
}
</
script
>
Controller:
public
class
AppliancesController : BaseApiController
{
public
AppliancesController(IRepository repo)
:
base
(repo)
{
}
public
DataSourceResult Get([System.Web.Http.ModelBinding.ModelBinder(
typeof
(WebApiDataSourceRequestModelBinder))]DataSourceRequest request)
{
//var appliances = TheRepository.GetAppliances().ToList()
// .Select(m => TheModelFactory.Create(m));
var appliances =
new
List<Appliance>() {
new
Appliance() { Title =
"Appliance 1"
, CurrentStatus = 2 } };
return
appliances.ToDataSourceResult(request);
}
public
HttpResponseMessage Get(
int
appId)
{
try
{
var entity = TheRepository.GetAppliance(appId);
if
(entity ==
null
)
return
Not_Found();
return
Request.CreateResponse(HttpStatusCode.OK,
TheModelFactory.Create(entity));
}
catch
{
// TODO logging
}
return
Bad_Request();
}
public
HttpResponseMessage Post([FromBody] Appliance model)
{
try
{
if
(!ModelState.IsValid)
return
Bad_Request();
model.CurrentStatus = (
int
)StatusType.Operational;
model.LastUpdated = DateTime.Now;
if
(TheRepository.Insert(model) && TheRepository.SaveAll())
{
return
Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(model));
}
}
catch
{
// TODO logging
}
return
Bad_Request();
}
[HttpDelete]
public
HttpResponseMessage Delete(
int
appId)
{
try
{
var entity = TheRepository.GetAppliance(appId);
if
(entity ==
null
)
return
Not_Found();
if
(TheRepository.DeleteAppliance(entity) && TheRepository.SaveAll())
{
return
Request.CreateResponse(HttpStatusCode.OK);
}
}
catch
{
// TODO logging
}
return
Bad_Request();
}
[HttpPut]
[HttpPatch]
public
HttpResponseMessage Patch(
int
appId, [FromBody] Appliance model)
{
try
{
if
(!ModelState.IsValid || appId != model.Id)
return
Bad_Request();
if
(TheRepository.UpdateAppliance(model) && TheRepository.SaveAll())
{
return
Request.CreateResponse(HttpStatusCode.OK);
}
}
catch
(Exception ex)
{
return
Bad_Request(ex);
}
return
Bad_Request();
}
}
The grid is displayed and the web api controller GET is being called and is returning the DataSourceResult and no rows are displayed.
If anyone can help I would be grateful. Thanks in advance.