I don't know how I can use linq query in bounding a grid column to display a value.
If I use this code
@(Html.Kendo().Grid<MYMODEL>(Model)
.Name("myObject")
.Columns(columns =>
{
columns.Bound(ViewBag["myList"].Where(p => p.Value == "2").Text);
I have an error : "cannot use lamba expression without casting before.
Any help will be very much appreciated
9 Answers, 1 is accepted
As the error states, you need to cast the object from the ViewData to a type that supports lambda expressions (for example, if it is a List: (ViewBag["myList"] as List<YourClass>).Where(p => p.Value == "2"))
Hope this helps.
Regards,
Konstantin Dikov
Telerik by Progress

Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.
But the thing that I want to achieve is a bit more complicated.
I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even is CODE is stored in the database table).
Controller.cs
public async Task<IActionResult> Index()
ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m => (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");
Razor View
...
columns.Bound(model => model.myCode); // For testing purpose
columns.Bound((((ViewBag["arType"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
// Error when executing
Any suggestion ?
...

Sorry some mistake were in the previous post.
Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.
But the thing that I want to achieve is a bit more complicated.
I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even if CODE is stored in the database table).
Controller.cs
public async Task<IActionResult> Index()
ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m => (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");
Razor View
...
columns.Bound(model => model.myCode); // For testing purpose
columns.Bound((((ViewBag["myList"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
// Error when executing
Any suggestion ?
I am not sure that I understand what is the result that you want to achieve. Within the bound column you could pass a string value that will match the name of the field that will be bound to the column.
If you want the column to be bound to one field, but to display the value for another field, the records in the grid should contain both values. The first field will be bound to the column and the second field can be displayed through a ClientTemplate (for example: ClientTemplate("#=SecondFieldName#")).
Hope this helps.
Regards,
Konstantin Dikov
Telerik by Progress

Hi Michel,
Were you able to find a workaround this situation? I've come across the same problem.

It is quite long time but in looking at my code here is the view (forgot the FooterTemplate if you don't need it):
columns.ForeignKey(model => model.CATEGORY_MEM, (System.Collections.IEnumerable)ViewBag.arCategory,"CODE_LFB", "LABEL_LFB") .ClientFooterTemplate("Total Branche : #=count#");
And in the Controller :
var category = _context.VG_LSTFEEDBACK_LFB.Where(f => f.NUMBER_LFB ==
"CAT"
).
Select(f =>
new
{
CODE_LFB = f.CODE_LFB,
LABEL_LFB = f.LABEL_LFB
});
ViewData[
"arCategory"
] = category.ToList();
The VG_LSTFEEDBACK_LFB is a SQL Table with different List value, it is why a use a where clause.
Hope it help's.

SQL Server Database MPD
CREATE TABLE [dbo].[VG_LSTFEEDBACK_LFB](
[GUID_LFB] [uniqueidentifier] NOT NULL,
[NAME_LFB] [varchar](100) NULL,
[NUMBER_LFB] [varchar](25) NULL,
[CODE_LFB] [int] NOT NULL,
[LABEL_LFB] [varchar](25) NULL,
CONSTRAINT [PK_VG_LSTFEEDBACK_LFB] PRIMARY KEY CLUSTERED

Michel, thank you for sharing the code and helping the community it is highly appreciated.
Antony, I`m glad to hear that the foreign key was helpful.
I can also suggest checking our foreign key demo and resources as they may prove helpful:
http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn
Kendo.Mvc.UI.Fluent/GridColumnFactory
https://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/configure-foreignkey-columns-combobox-edit-template
Regards,
Stefan
Progress Telerik