Asp.net MVC 2 JqueryGrid表空行

时间:2010-10-23 09:36:57

标签: asp.net-mvc vb.net asp.net-mvc-2

我需要一些asp.net mvc的帮助

控制器:

<HandleError()> _
Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        ViewData("Message") = "Welcome to ASP.NET MVC!"

        Return View()
    End Function

    Public Function GetMovieData() As ActionResult
        Dim sortColumn As String = (Request.Params("sidx")).ToString()
        Dim sortOrder As String = (Request.Params("sord")).ToString()
        Dim pageIndex As Integer = Convert.ToInt32(Request.Params("page"))
        'Remember this is NOT 0 based
        Dim rowCount As Integer = Convert.ToInt32(Request.Params("rows"))
        Dim movies As New Movies()
        Dim movieList = movies.GetMovies()
        Dim totalRecords As Integer = movieList.Count()
        Dim totalPages As Integer = CInt(Math.Ceiling(CSng(totalRecords) / CSng(rowCount)))

        ' Dim allRecords As IQueryable(Of Movie)
        ' allRecords = movieList.OrderBy(Function(movie) movie.Id).Skip((pageIndex - 1) * rowCount).Take(rowCount)
        'movieList = movieList.AsQueryable().OrderBy(Function(movie) movie.Id).Skip((pageIndex - 1) * rowCount).Take(rowCount) 
        Dim finalList = movies.Sort(movieList, sortColumn, sortOrder).Skip((pageIndex - 1) * rowCount).Take(rowCount)

        Dim jsonData = New With { _
                .total = totalPages, _
                .page = pageIndex, _
                .records = totalRecords, _
                .rows = (From m In finalList _
                    Select New With { _
                        .i = m.Id, _
                        .cell = New String() {m.Id.Value.ToString(), m.Name, m.Director, m.ReleaseDate.ToShortDateString(), m.IMDBUserRating, m.Plot, m.ImageURL} _
                    } _
                ).ToList()
                        }
        Return Json(jsonData)
    End Function

    Function About() As ActionResult
        Return View()
    End Function
End Class

查看:

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="PageHead" ContentPlaceHolderID="Head" runat="server" >
<link rel="stylesheet" type="text/css" media="screen" href="../../Scripts/grid/css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../Scripts/grid/redmond/jquery-ui-1.8.2.custom.css" />
<script src="../../Scripts/grid/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/grid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../Scripts/grid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Html.Encode(ViewData("Message")) %></h2>

    <script type="text/javascript">
        $(function () {
            $("#list").jqGrid({
                url: '/Home/GetMovieData',
                datatype: 'json',
                mtype: 'GET',
                colNames: ['id', 'Movie Name', 'Directed By', 'Release Date', 'IMDB Rating', 'Plot', 'ImageURL'],
                colModel: [
                  { name: 'id', index: 'Id', width: 55, sortable: false, hidden: true },
                  { name: 'Movie Name', index: 'Name', width: 250 },
                  { name: 'Directed By', index: 'Director', width: 250, align: 'right' },
                  { name: 'Release Date', index: 'ReleaseDate', width: 100, align: 'right' },
                  { name: 'IMDB Rating', index: 'IMDBUserRating', width: 100, align: 'right' },
                  { name: 'Plot', index: 'Plot', width: 55, hidden: true },
                  { name: 'ImageURL', index: 'ImageURL', width: 55, hidden: true}],
                pager: '#pager',
                rowNum: 10,
                rowList: [10, 20, 30],
                sortname: 'invid',
                sortorder: 'desc',
                viewrecords: true,
                caption: 'My first grid'
            });
        }); 
</script>
<table id="list"></table> 
<div id="pager"></div> 


    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
</asp:Content>

当我运行此代码时,表格始终为空。

1 个答案:

答案 0 :(得分:0)

GetMovieData操作中返回JSON时,请确保allow GET requests,因为默认情况下会禁用它们:

Return Json(jsonData, JsonRequestBehavior.AllowGet)

我建议您使用FireBug,它允许您查看AJAX请求和可能的错误消息。调试这样的问题要容易得多。