Hi guys,
I have a simple example I have created to try get the kendo stuff working stably on my machine before I think about purchasing it. However, I am struggling to get simple behaviour working which am sure should not be a problem. I am using MVC and what I am trying to do is to refresh my grid below my drop down when I select a Supplier in the dropdown. My grid is populated with products and would want it to reduce the collection and show only the products for a supplier. Code Below:
Partial View 1:
Please ignore the helper classes as these return the correct data, How ever, I am able to get the id coming through if I put a breakpoint but after 'Json(kendoResult, JsonRequestBehavior.AllowGet);' nothing happens and my grid remains the same, Please assist?
I have a simple example I have created to try get the kendo stuff working stably on my machine before I think about purchasing it. However, I am struggling to get simple behaviour working which am sure should not be a problem. I am using MVC and what I am trying to do is to refresh my grid below my drop down when I select a Supplier in the dropdown. My grid is populated with products and would want it to reduce the collection and show only the products for a supplier. Code Below:
Partial View 1:
@using Kendo.Mvc.UI
@model IEnumerable<NorthwindKendo.ViewModels.SupplierViewModel>
<div id="supplier" style="width: 300px;">
@(Html.Kendo().DropDownList()
.Name("suppliers")
.DataTextField("CompanyName")
.DataValueField("SupplierID")
.Events(e => e.Change("change"))
.BindTo(Model)
.SelectedIndex(-1)
)
</div>
<script>Product Controller :
function change() {
var filterGrid = $("#grid").data("kendoGrid");
var valu = $("#suppliers").val();
$.ajax({
url: "/Product/Grid",
dataType: "json",
data: { supplierId: valu },
success: function (result) {
//filterGrid.dataSource.read();
//filterGrid.dataSource.fetch();
//alert("OK");
$("#grid").data("kendoGrid").dataSource.read();
//filterGrid.dataSource.read();
filterGrid.refresh();
}
});
};
</script> Partial View 2:@model IEnumerable<NorthwindKendo.ViewModels.ProductViewModel>
<div id="clientsDb">
@(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.ProductID).Visible(false).Groupable(false);
columns.Bound(p => p.ProductName)
.ClientFooterTemplate("Total Products: #=count#");
columns.Bound(p => p.QuantityPerUnit);
columns.Bound(p => p.SupplierID);
columns.Bound(p => p.ReorderLevel);
columns.Bound(p => p.UnitPrice).Width(90);
columns.Bound(p => p.UnitsInStock).Width(120);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Navigatable()
.ColumnMenu()
.Resizable(resize => resize.Columns(true))
.Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model =>
{
model.Id(info => info.ProductID);
model.Field(p => p.ProductID).Editable(false);
model.Field(p => p.ProductName).Editable(true);
model.Field(p => p.QuantityPerUnit).Editable(true);
model.Field(p => p.UnitPrice).Editable(true);
model.Field(p => p.SupplierID).Editable(false);
})
.Aggregates(aggregates =>
{
aggregates.Add(p => p.ProductName).Count();
})
.Events(events => events.Error("error_handler"))
.PageSize(10)
.Create(update => update.Action("Create", "Product"))
.Read(read => read.Action("Grid", "Product"))
.Update(update => update.Action("Save", "Product"))
.Destroy(update => update.Action("Delete", "Product"))
)
)
</div>
<script type="text/javascript">
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
</script>
public class ProductController : Controller
{
public ProductController()
{
Mapper.CreateMap<Product, ProductViewModel>();
Mapper.CreateMap<Category, CategoryViewModel>();
Mapper.CreateMap<Supplier, SupplierViewModel>();
}
public List<SupplierViewModel> Suppliers
{
get
{
var viewModels = Repository.GetViewModelCollection<Supplier, SupplierViewModel, NorthWindDataContext>(); ;
return viewModels;
}
}
//
// GET: /Product/
public ActionResult Index()
{
ViewData["Suppliers"] = Suppliers;
return View();
}
public JsonResult Grid()
{
ViewData["Suppliers"] = Suppliers;
var supplierId = Request["supplierId"];
var viewModels = ProductControllerHelper.GetViewModels<Product, ProductViewModel, NorthWindDataContext>(supplierId, info => info.SupplierID == Convert.ToInt32(supplierId));
var kendoResult = new Kendo.Mvc.UI.DataSourceResult
{
Data = viewModels,
Total = viewModels.Count()
};
return Json(kendoResult, JsonRequestBehavior.AllowGet);
}
}
Please ignore the helper classes as these return the correct data, How ever, I am able to get the id coming through if I put a breakpoint but after 'Json(kendoResult, JsonRequestBehavior.AllowGet);' nothing happens and my grid remains the same, Please assist?