.NET MVC2+ LINQ + JQGRID 範例

目前開發.NET MVC2專案,

開發環境 .NET 3.5, 使用C#

 

View:

先至http://www.trirand.com/blog/?page_id=6

下載jqgrid相關js

 

在Site.Master include

   

  

 

在Index.aspx


   
   


       

       

   

 

Controller:

 

public ActionResult Index()
{
        return View();
}

 

public JsonResult GetGrid(string sidx, string sord, int page, int rows)
{
     var dc= new dc();
     var query = from emp in dc.employee
                        select new employee_a
                        {
                           a= emp .a,
                           b = emp .b,
                           c = emp .c,
                        };
          
      int pageIndex = Convert.ToInt32(page) - 1;
      int pageSize = rows;
      int totalRecords = query.Count();
      var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
      var resultSet = query.ToList();

      IQueryable items = resultSet.AsQueryable().OrderBy(sidx+" "+sord).Skip(pageIndex *    pageSize).Take(pageSize); //此處orderby會有問題, 需再using System.Linq.Dynamic;
          
      var jsonData = new
           {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (from item in items
                             select new
                                  {
                                            i = item.a,
                                           cell = new []
                                                       {
                                                                 item.a.ToString(),
                                                                 item.b,
                                                                 item.c.ToString()
                                                        }
                                   }).ToArray()
            };
            return Json(jsonData ,JsonRequestBehavior.AllowGet);
        }

 

 

 

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditGrid(int id,string oper, string a, string b, string c)
{
       var dc= new dc();
       string IN_LOCATION = Convert.ToString(HttpContext.Request.Form["IM_CONTAINER_IN_LOCATION"]);
       // Get the data from and find the Order corresponding to the edited row
       employee employee = (from c in dc.T_IM_employee
                                            where c.a == id
                                            select c).First();

        if (!(employee == null))
        {
                // update the employee information
                employee .a = a;
                employee .b = b;
                employee .c = c;
               
                dc.SubmitChanges();
                return Content("true");
         }
         else
         {
                return Content("false");
         }
}


 

你可能感兴趣的:(ASP,.NET,MVC2,mvc,.net,linq,stylesheet,string,jquery)