0

我想做的就是这样。
在我提交表单后,表单中的值被传递给控制器​​,这些值被转换并返回到数据表中。

我知道如何将 json 数据从控制器返回到数据表。
我也知道如何通过 Ajax.BeginForm 将表单值传递给控制器​​。
但我不知道如何做到这两点。
这是我的代码,它只是将固定的 json 数据返回到数据表。

看法

    <script type="text/javascript">

        function PullIntoTable() {
            $('#example').dataTable({
                "bDestroy": true,
                "bAutoWidth": false,
                "aoColumns": [
                { sWidth: '30%' },
                { sWidth: '10%' },
                { sWidth: '20%' },
                { sWidth: '20%' },
                { sWidth: '20%' }, ],
                "sAjaxSource": '@Url.Action("Search", "SearchCompany")',
                "fnServerData": function (sSource, aoData, fnCallback) {
                    $.ajax({
                        dataType: 'json',
                        type: "POST",
                        url: sSource,
                        data: aoData,
                        success: function (json) {
                            fnCallback(json);
                        }
                    })
                }
            });
        };

    </script>

    <input type="submit" value="Search" onclick="PullIntoTable()" />


<table cellpadding="0" cellspacing="0" border="0" class="search_result" id="example" width="100%">
    <thead>
        <tr>
            <th>Company</th>
            <th>Location</th>
            <th>Address</th>
            <th>Status</th>
            <th>Inactive</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
    <tfoot>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </tfoot>
</table>

控制器

public class SearchCompanyController : Controller
{
    [HttpPost]
    public ActionResult Search()
    {
        var Result = new List<Result>()
        {
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
            new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"}
        };

        return Json(
            new { aaData = Result.Select(x => new[] { x.Name, x.Location, x.Address, x.Status, x.Inactive }) },
            JsonRequestBehavior.DenyGet
            );
    }
}

public struct Result
{
    public string Name { get; set; }
    public string Location { get; set; }
    public string Address { get; set; }
    public string Status { get; set; }
    public string Inactive { get; set; }
}
4

2 回答 2

1

Datatables 需要非常特定格式的数据。

有一个使用 OpenSearch 的示例,您可以收集详细信息以用于您的特定实现(即,您在 javascript 中为回调构造 json 对象的非数据表特定实现)。这个解决方案对我最有吸引力,因为它将特定于数据表的实现细节保留在服务器端代码之外。

datatables.net 站点上已经有 asp.net示例,它们基本上可以为您提供所需的一切,尽管采用了相当丑陋的匈牙利符号格式。

我手头没有样本,但我确实获取了 c# 样本并将其转换为动作过滤器,因此我不必处理匈牙利符号。如果我把手放在它上面,并且你有兴趣,我会看看我是否可以把它贴在这里。

于 2012-04-26T01:27:23.210 回答
0

解决了。
我可以从控制器获取值并将它们拉入数据表,如下所示

查看脚本

function PullIntoTable(e) {
    $('#example').dataTable({
        "bDestroy": true,
        "bAutoWidth": false,
        "aoColumns": [
        { sWidth: '30%' },
        { sWidth: '10%' },
        { sWidth: '20%' },
        { sWidth: '20%' },
        { sWidth: '20%' }, ],
        "aaData": e.aaData
    });
};
于 2012-04-27T01:27:37.300 回答