json数据显示不正确

时间:2012-05-23 06:00:27

标签: json asp.net-mvc-3 flexigrid

我使用了flexigrid,并且应该在其中显示的数据显示在白页中,图像显示在这里:enter image description here

这是我的观点:

@using (Html.BeginForm("JsonEmployee", "Employees", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <h5>
            ENTER A NAME TO FILTER THE LIST:</h5>
        <div style="float: left">
            <input name="nameSelected" type="text" /> &nbsp  </div>
        <div style="float: left">
            <input class="btn btn-inverse" type="submit" value="FILTER" /></div>
    </fieldset>
}
<table class="flex" style="display: none">
</table>

<script type="text/javascript" language="javascript">
$('.flex').flexigrid({
    url: '/Employees/JsonEmployee',
    dataType: 'json',
    method: 'GET',
    colModel: [
{ display: 'NUMBER', name: 'number', width: 200, sortable: true, align: 'center' },
{ display: 'NAME', name: 'name', width: 300, sortable: true, align: 'center' },
{ display: 'ROLE', name: 'role', width: 200, sortable: true, align: 'center'}],
    searchitems: [
    { display: 'NUMBER', name: 'number' },
    { display: 'NAME', name: 'name', isdefault: true }
    ],
    sortname: "number",
    sortorder: "name",
    usepager: true,
    title: 'Employees',
    useRp: true,
    rp: 15,
    showTableToggleBtn: true,
    width: 950
});
</script>

这是我的控制器:

    [Authorize(Users = "Admin")]
    [HttpPost]
    public ActionResult JsonEmployee(String nameSelected)
    {
        CacheClear();
        var employees = db.Employees.Where(r => r.Name.Contains(nameSelected)).OrderBy(r => r.Name);
        var res = new
        {
            page = 1,
            total = employees.Count(),
            rows = employees.Select(x => new { x.Number, x.Name, x.Role })
                .ToList()
                .Select(x => new
                {
                    id = x.Number,
                    cell = new string[]
                    {
                    x.Number,
                    x.Name,
                    x.Role
                    }
                }).ToArray(),
        };
        return Json(res, JsonRequestBehavior.AllowGet);
    }

我有一个接受用户输入字符串的表单..如果用户单击提交按钮,我的页面中的flexigrid应该由过滤列表填充..但是,页面重定向到带有数据的白页像上面的图片一样json ...

1 个答案:

答案 0 :(得分:0)

您已创建指向/Employees/JsonEmployee操作的HTML表单。因此,当您提交此表单时,浏览器将向此控制器操作发送POST请求并重定向到该控制器操作并显示其执行结果是正常的。这就是HTML的工作原理。如果您想要保持在同一页面上,您可以AJAXify此表单并取消默认操作。像这样:

$('form').submit(function () {
    // Send an AJAX request to the controller action that this HTML <form>
    // is pointing to in order to fetch the results in a JSON form
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function (result) {
            // When the AJAX request succeeds we get the result returned
            // by the controller action which represents the JSON data source
            // for the flexgrid. So all that's left is rebind the grid with
            // this new data source:
            $('.flex').flexAddData(result);
        }
    });

    // Prevent the browser from redirecting to the /Employees/JsonEmployee action
    return false;
});