How to load JSON data into SlickGrid with Razor MVC-4

跟風遠走 提交于 2020-01-23 17:53:05

问题


I am new to jquery, slick grid and razor. I have gone through SlickGrid examples and am able to load static data into a SlickGrid. Now I am trying to load JSON data from MSSQL into my SlickGrid. I have seen a few examples online, but I believe I am missing something not mentioned in those examples.

Here is what I have code.

SlickGridProducts.js var grid;

var columns = [
    { id: "ProductID", name: "ProductID", field: "ProductID", width: 50 },
    { id: "ItemDesc", name: "ItemDesc", field: "ItemDesc", width: 200 },
    { id: "DivName", name: "DivName", field: "DivName", width: 50 },
    { id: "DeptDesc", name: "DeptDesc", field: "DeptDesc", width: 75 },
    { id: "ClassDesc", name: "ClassDesc", field: "ClassDesc", width: 100 },
    { id: "SubClassDesc", name: "SubClassDesc", field: "SubClassDesc", width: 100 }
];
var options = {
    editable: true,
    enableAddRow: true,
    enableCellNavigation: true,
    asyncEditorLoading: false,
    autoEdit: false
};

$(function () {

    var slickdata = [];
    $.getJSON("/Products/GetSlickGridData", function (items) {
        for (var i = 0; i < items.length; i++) {
            slickdata[i] = {
                ProductID: items[i].ProductID,
                ItemDesc: items[i].ItemDesc,
                DivName: items[i].DivName,
                DeptDesc: items[i].DeptDesc,
                ClassDesc: items[i].ClassDesc,
                SubClassDesc: items[i].SubClassDesc
            };
        }
    });

    grid = new Slick.Grid("#myGrid", slickdata, columns, options);
    grid.setSelectionModel(new Slick.RowSelectionModel());

    grid.setActiveCell(0, 0);
})

Products/Index.cshtml

@model IEnumerable<JQGrid.Models.Product>

@{
    ViewBag.Title = "Index";
}

<link href="@Url.Content("~/Content/slick.grid.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.event.drag.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SlickGrid/slick.core.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SlickGrid/slick.grid.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SlickGridProducts.js")" type="text/javascript"></script>

<h2>Index</h2>

<div id="myGrid" style="width:800px;height:300px;"></div>

ProductsController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JQGrid.Models;

namespace JQGrid.Controllers
{
    public class ProductsController : Controller
    {
        private ProductPickerEntities db = new ProductPickerEntities();

        //
        // GET: /Products/

        public ActionResult Index()
        {
            return View(db.Products.ToList());
        }

...

        public JsonResult GetSlickGridData()
        {
            var slickGridData = db.Products.ToList();
            return Json(slickGridData, JsonRequestBehavior.AllowGet);
        }
    }
}

If I add a breakpoint in JsonResult GetSlickGridData() and watch slickGridData, I see that it is populated with all the items I want in my slickgrid.

With this all I get is a blank white box for my slick grid. I figure the problem is in my js where I am filling slickdata, but not sure what to fix.

**** Revision *****

I found one of my issues, but the slickgrid is still blank. My issue was the json result being returned was too large. So I modified my ProductsController.cs code for right now to say

public JsonResult GetSlickGridData()
{
    var slickGridData = db.Products.ToList();
    var jsonResult = Json(slickGridData, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
}

This resolves the maxlength error although I had thought this was resolved in MVC-4.


回答1:


Did you check the browser console for errors?




回答2:


You're initializing grid.setSelectionModel(new Slick.RowSelectionModel());, but have not included a reference to it.

It is slick.rowselectionmodel.js and is typically under the Plugins folder.



来源:https://stackoverflow.com/questions/15344293/how-to-load-json-data-into-slickgrid-with-razor-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!