jeudi 7 janvier 2016

How to bind a telerik grid to a controller action

The following code is returning me the data in JSON in the browser, but its not displaying the telerik mvc grid.

public class TipoDeCanalesController : GenericController
{
    private UnitOfWork unitOfWork = new UnitOfWork();

    // GET: TipoDeCanales
    public ActionResult Index([DataSourceRequest] DataSourceRequest request)
    {
        return Json(unitOfWork.TipoDeCanalRepository.Get(),JsonRequestBehavior.AllowGet);
    }

and the view

@model IEnumerable<powerdata.comisiones.models.tipodecanal>

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    @(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ID);
        columns.Bound(p => p.Nombre).Title("Nombre");
        columns.Bound(p => p.Descripcion).Title("Descripcion");
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable(scr => scr.Height(430))
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.ID))
        .Create(update => update.Action("EditingPopup_Create", "Grid"))
        .Read(read => read.Action("Index", "Grid"))
        .Update(update => update.Action("EditingPopup_Update", "Grid"))
        .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
     )
    )
    %>
    <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>

Aucun commentaire:

Enregistrer un commentaire