【问题标题】:How to Bind HandsonTables in MVC如何在 MVC 中绑定 HandsonTables
【发布时间】:2012-09-14 02:59:47
【问题描述】:

我正在使用 MVC4 开发一个 Web 应用程序。我在 UI 中有 HandsOnTable。我尝试使用 json 绑定数据,但不能。并且不告诉显示错误。这是代码sn-p

$.ajax({
            url: "/Customer/GetSpreadSheetGrid",
            type: "GET",
            dataType: "json",
            data: { customerId: customerID }
        })
            .success(function (result) {
    var vSpreadSheet = document.getElementById("SpreadSheetgrid");
        $("#SpreadSheetgrid").handsontable("loadData", result);
    })
    .fail(function (r, o) {
        alert("Failed : " + r.responseText);
    });
    }

这是我的控制器方法

public JsonResult GetSpreadSheetGrid(int customerId)
    {
IEnumerable<tblCustomerSpreadsheetInfo> resResult = null;
// Calling SP
resResult = _customer.GetCustomerCustomInfoByCustomerID(customerId);
return Json(resResult, JsonRequestBehavior.AllowGet);
    }

这段代码有什么错误。

【问题讨论】:

  • 这里失败了.. ajax 还是 handsontable?

标签: jquery ajax asp.net-mvc handsontable


【解决方案1】:
<div id="dataTable" class="dataTable"></div>    
$("#dataTable").handsontable({
       rows: 6,
       cols: 8,
       contextMenu: true,
       colHeaders: true
    });
    var url = "/Home/GridData";
    $.get(url, null, function (data) {
    $("#dataTable").handsontable("loadData", data);
    strong text});

这是 HomeController 中的方法

public JsonResult GridData()
        {
            var jsonData = new[]
                         {
                             new[] {"1", "4", "Is this a good question?"},
                             new[] {"2", "5", "Yes."},
                             new[] {"3", "6", "It is!"}
                        };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

我使用此代码实现了将控制器方法绑定到掌上电脑。 但我真的很难获取数据并将其作为 string[][] 传递给控制器​​方法。

【讨论】:

    【解决方案2】:

    如下修改你的jquery ajax(在成功和失败函数中保留一个调试器。使用IE和Visual Studio调试它。如果有错误,你可以看到。

    $.ajax({
            url: "/Customer/GetSpreadSheetGrid",
            type: "GET",
            data: { customerId: customerID },
            contentType: "application/json; charset=utf-8",
            success: function (_results) {
                 debugger;
                var vSpreadSheet = document.getElementById("SpreadSheetgrid");
                $("#SpreadSheetgrid").handsontable("loadData", result);
            },
            error: function (_results, status) {
                debugger;
            }
        });
    

    【讨论】:

    • 愚蠢的问题:使用 ASP.NET MVC,第一个网页上的表格的原始呈现是否正常由视图呈现?我知道后续分页是使用返回 JSON 但在 web 开发方面没有太多经验的 ajax 调用完成的,我无法完全想象它是如何工作的。服务器是否会使用嵌入在动态创建的 javascript 中的 json 数据动态构建第一页?我想不是。我想知道当我们将表初始化为handsontable 时,我们如何获得第一个数据绑定。对不起我的无知
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多