Model
Controller
View
I get a grid, but there are no rows in it for the 3 records known to be in 'FilteredSwitches_Read()'.
What am I doing wrong?
Dave
public
class
Switch
{
[DisplayName(
"Switch Id"
)]
public
string
SwitchId {
get
;
set
; }
}
Controller
public
class
UserConsoleViewModelsController : Controller
{
public
ActionResult FilteredSwitches_Read([DataSourceRequest]DataSourceRequest request)
{
var filteredSwitches = GetFilteredSwitches().ToDataSourceResult(request);
//3 records are here at runtime
return
Json(filteredSwitches);
}
private
IEnumerable<Switch> GetFilteredSwitches()
{
return
db.Switches.ToList();
}
}
View
@(Html.Kendo().Grid<Switch>()
.Name(
"grid"
)
.Columns(columns =>
{
columns.Bound(c => c.SwitchId).Width(140);
})
.HtmlAttributes(
new
{ style =
"height: 380px;"
})
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(
true
)
.PageSizes(
true
)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action(
"FilteredSwitches_Read"
,
"UserConsoleViewModels"
))
)
I get a grid, but there are no rows in it for the 3 records known to be in 'FilteredSwitches_Read()'.
What am I doing wrong?
Dave
5 Answers, 1 is accepted
0
Accepted
Hello Dave,
The sample code looks correct. I would suggest you to check the developer tools console for JavaScript errors and also the network tab to assure that the request to the FilteredSwitches_Read action was successful.
I am looking forward to hearing from you.
Regards,
Dimiter Madjarov
Telerik
The sample code looks correct. I would suggest you to check the developer tools console for JavaScript errors and also the network tab to assure that the request to the FilteredSwitches_Read action was successful.
I am looking forward to hearing from you.
Regards,
Dimiter Madjarov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
0
IPV
Top achievements
Rank 1
answered on 28 Nov 2014, 04:32 PM
There was an error indicated in the dev tools... apparently there was something about my model that the Json-ifier didn't like. Thinking I was using the control incorrectly, I presented a simplified version of my model in my first post. I am now using that simplified model and it is working. Now to slowly work toward the more complex model until I find the issue.
Thank you!
Thank you!
0
Hello Dave,
Probably the reason was a circular reference. If that is the case you should extract a ViewModel containing only the needed properties. More information is available in the documentation.
Regards,
Dimiter Madjarov
Telerik
Probably the reason was a circular reference. If that is the case you should extract a ViewModel containing only the needed properties. More information is available in the documentation.
Regards,
Dimiter Madjarov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
0
IPV
Top achievements
Rank 1
answered on 29 Nov 2014, 05:27 AM
Yes, a Circular Reference was indeed the error and indeed I did solve it with a less complex view model. Now I have another issue. Please let me know if I should post this as a new question.
My ViewModel looks like this.
Controller
This works fine on first render, on refresh, of if I sort on either of the two string 'Id' columns, but if I sort on either of the DateTime columns I get this error:
How do you suggest I do this? Please know that the value of either/both the two DateTime fields could change between sorts.
Thanks!
My ViewModel looks like this.
public
class
UserConsoleSwitchGridModel
{
public
string
SwitchId {
get
;
set
; }
public
string
SentinelId {
get
;
set
; }
public
DateTime MostRecentNormal
{
get
{
var db =
new
ApplicationDbContext();
try
{
var throws = db.SwitchThrows
.Where(s => s.SwitchId == SwitchId)
.Where(a => a.Action ==
"Normal"
)
.OrderByDescending(t => t.Timestamp)
.Take(1);
var _throw = throws.ToList().ElementAt(0);
return
_throw.Timestamp;
}
catch
{
return
DateTime.MinValue;
}
}
}
[DisplayName(
"Most Recent Reverse Throw"
)]
public
DateTime MostRecentReverse
{
get
{
{
var db =
new
ApplicationDbContext();
try
{
var throws = db.SwitchThrows
.Where(s => s.SwitchId == SwitchId)
.Where(a => a.Action ==
"Reverse"
)
.OrderByDescending(t => t.Timestamp)
.Take(1);
var _throw = throws.ToList().ElementAt(0);
return
_throw.Timestamp;
}
catch
{
return
DateTime.MinValue;
}
}
}
}
}
Controller
public
ActionResult FilteredSwitches_Read([DataSourceRequest]DataSourceRequest request)
{
var filteredSwitches = GetFilteredSwitches().ToDataSourceResult(request);
return
Json(filteredSwitches);
}
private
IEnumerable<UserConsoleGridModel> GetFilteredSwitches()
{
var userid = User.Identity.GetUserId();
var user = db.Users.First(u => u.Id == userid);
return
db.Switches.Include(i => i.Sentinel).Include(n => n.MostRecentNormalSwitchThrow).Include(r => r.MostRecentReverseSwitchThrow)
.Where(c => c.CompanyId == user.Company.CompanyId)
.Select(x =>
new
UserConsoleGridModel()
{
SwitchId = x.SwitchId,
SentinelId = x.SentinelId
});
}
@(Html.Kendo().Grid<
UserConsoleGridModel
>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.SwitchId).Width(140);
columns.Bound(c => c.SentinelId);
columns.Bound(c => c.MostRecentNormal);
columns.Bound(c => c.MostRecentReverse);
})
.HtmlAttributes(new { style = "height: 380px;" })
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("FilteredSwitches_Read", "UserConsoleViewModels"))
)
)
This works fine on first render, on refresh, of if I sort on either of the two string 'Id' columns, but if I sort on either of the DateTime columns I get this error:
The specified type member
'MostRecentReverse'
is
not supported
in
LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
How do you suggest I do this? Please know that the value of either/both the two DateTime fields could change between sorts.
Thanks!
0
Accepted
Hello Dave,
The current problem does not seem directly related to Kendo UI, but to the way the data is retrieved. I found similar forum threads (for example here and here), that could help in resolving it.
Regards,
Dimiter Madjarov
Telerik
The current problem does not seem directly related to Kendo UI, but to the way the data is retrieved. I found similar forum threads (for example here and here), that could help in resolving it.
Regards,
Dimiter Madjarov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.