When the partial view is rendered, it only displays an empty grid without data in it.
Can't i do an server side Ajax binding (json) for the grid in a partial View ?
This only works when the grid is on a view associated with Home controller and not in a partial view.
How do i get it to work in a partial view ?
Below is what i have in the Partial View.
@using Kendo.Mvc.UI
<div id="Main">
Kendo Grid Partial View Demo
</div>
<br />
<div id="Grid">
@(Html.Kendo().Grid<Mvc4SampleApplication.Models.Product>()
.Name("grid")
.Pageable(pager => pager
.Messages(messages => messages.Empty("No data")))
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax()// Specify that ajax binding is used
.Read(read => read.Action("Products_Read", "Home")) // Set the action method which will return the data in JSON format
//.Model(model => model.Id(product => product.ProductID))
.Model(model =>
{
//The unique identifier (primary key) of the model is the ProductID property
model.Id(product => product.ProductID);
// Declare a model field and optionally specify its default value (used when a new model instance is created)
model.Field(product => product.ProductName).DefaultValue("N/A");
// Declare a model field and make it readonly
model.Field(product => product.UnitsInStock).Editable(false);
}
)
)
.Columns(columns =>
{
// Create a column bound to the ProductID property
columns.Bound(product => product.ProductID);
// Create a column bound to the ProductName property
columns.Bound(product => product.ProductName).ClientTemplate("<strong>#: ProductName #</strong>");
// Create a column bound to the UnitsInStock property
columns.Bound(product => product.UnitsInStock);
// Define a template column - it needs a templated razor delegate
columns.Template(@<text> @Html.ActionLink("About", "Home", new { id = item.ProductID }) </text>);
})
.Pageable() // Enable paging
.Sortable() // Enable sorting
)
</div>
And below is the code to get Json Data for kendo grid in the Home Controller in my MVC app.
public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
List<Product> ProductList = new List<Product>();
for (int i = 1; i <= 10; i++)
{
Product p = new Product();
p.ProductID = i;
p.ProductName = "Product_" + i;
p.UnitsInStock = 5;
ProductList.Add(p);
}
{
IEnumerable<Product> products = ProductList;
DataSourceResult result = products.ToDataSourceResult(request);
return Json(result);
}
}
Any Help will be greatly appreciated.
Thanks in Advance!