ASP.NET MVC 4 – ( MVCGrid.Net ) 方法

ASP.NET MVC 4 – ( MVCGrid.Net ) 方法

Installation :
Download the Package by NuGet. The File ( "\App_Start\MVCGridConfig.cs" ) would be created during the Package Installation.

   Master Page
 
   <head>
 
      … …
 
      @Scripts.Render(" ~/bundles/jquery ")
      @RenderSection("scripts", required: false)
 
      < script src=" ~/MVCGridHandler.axd/script.js " >< /script >
 
   </head>
 
   View
 
   … …
 
   @Html.MVCGrid("EmployeeGrid")
 
   \App_Start\MVCGridConfig.cs
 
   public static void RegisterGrids()
   {
 
      MVCGridDefinitionTable.Add("EmployeeGrid", new MVCGridBuilder<Person>()
         .WithAuthorizationType(AuthorizationType.AllowAnonymous)
         .AddColumns(cols =>
         {
            cols.Add("Id").WithValueExpression(p => p.Id.ToString());
            cols.Add("FirstName").WithSorting(true).WithHeaderText("First Name").WithValueExpression(p => p.FirstName);
            cols.Add("LastName").WithHeaderText("Last Name").WithValueExpression(p => p.LastName);
         })
         .WithSorting(true, "FirstName")
         .WithRetrieveDataMethod((options) =>
         {
            List<Person> list = new List<Person>();
 
            list.Add(new Person("1", "Peter", "Chan"));
            list.Add(new Person("2", "Linda", "Wong"));
            list.Add(new Person("3", "Andy", "Ho"));
            list.Add(new Person("4", "Jordon", "Li"));
            list.Add(new Person("5", "Ken", "Fong"));
            list.Add(new Person("6", "Stephen", "Sung"));
 
            var result = new QueryResult<Person>();
 
            if (options.QueryOptions.SortDirection == SortDirection.Asc)
               result.Items = list.OrderBy(p => p.FirstName).ToList();
            else if (options.QueryOptions.SortDirection == SortDirection.Dsc)
               result.Items = list.OrderByDescending(p => p.FirstName).ToList();
            else
               result.Items = list;
 
            return result;
         })
      );
 
      … …
 
      MVCGridDefinitionTable.Add("ActingGrid", … …
 
      );
 
   }