Reputation: 4412
I've got a Kendo grid Razor defined:
@(Html.Kendo().Grid<MyApp.Models.FolderName.Whatever>()
.Name("grid")
...
...
Since I'm trying to create reusable pages, I needed to define the model of the grid in the controller and pass it via ViewBag.
@(Html.Kendo().Grid<ViewBag.myModel>()
.Name("grid")
...
...
But the above attempt fails:
It doesn't need to be via ViewBag but I need to define the model in some other way.
Upvotes: 1
Views: 1531
Reputation: 12856
I think your design is flawed, and you shouldn't be using viewbags at all.
Personally I dislike passing data to the view directly, and the Kendo Grid is perfectly capable of retrieving data itself. This can be as generic as you want it to be.
If you have different models with the same structure, but filled differently you should combine them into a single reusable generic model.
@(Html.Kendo().Grid<MyApp.Models.GenericModel>()
.Name("grid")
.DataSource(ds => ds.Read("GetGenericData", "Home", new { someParameter })
...
)
Upvotes: 1
Reputation: 1982
I'd pass the strongly typed model into Razor view to avoid casting.
@model IEnumerable<MyModel>
then it will be like
@(Html.Kendo().Grid<MyModel>()
.Name("grid")
...
If you want to stick to ViewBag approach, then you will have to cast the data
@(Html.Kendo().Grid((IEnumerable<MyModel>)ViewBag.myModel)
.Name("Grid")
...
Telerik has done a good job of documenting this. You can find it here.
Please note that MyModel
here is a strongly typed one.
Edit
Based on comments, see if this helps you. My guess is that you have some common properties that can go in a base class like below
public class BaseModel
{
public string PropertyOne{get;set;}
public string PropertyTwo{get;set;}
}
Then create child models like below:
public class WhateverModel:BaseModel
{
}
public class MyModel:BaseModel
{
}
Now, you can either have collection of WhateverModel
or MyModel
in ViewBag.myModel
Your view code would be like below:
@(Html.Kendo().Grid((IEnumerable<BaseModel>)ViewBag.myModel)
.Name("Grid")
...
This is how you can pass any model collection into your ViewBag.myModel as long as that model is inheriting from the BaseModel
I hope this helps.
Upvotes: 1
Reputation: 3139
syntax would be like this
@(Html.Kendo().Grid((IEnumerable<MyApp.Models.FolderName.Whatever>)ViewBag.myModel)
Upvotes: 0