※ datagrid的基本属性和方法
※ datagrid分页在前后台的实现
最终效果:
与视图显示对应的view model
public string ItemId { get; set; }
public string ProductId { get; set; }
public decimal ListPrice { get; set; }
public decimal UnitCost { get; set; }
public string Attr1 { get; set; }
public Int16 Status { get; set; }
模拟一个从数据库拿数据,并考虑分页的服务层方法
□ 与分页有关的类
public int PageSize { get; set; }
public int PageIndex { get; set; }
在实际项目中,可以把以上作为一个基类,把各个领域的各种搜索条件封装成继承PageParam的子类。
□ 分页服务层方法
using DataGridInMvc.Models;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities;
namespace DataGridInMvc.Helper
{
public class BookService
{
public IEnumerable<Book> LoadPageBookData(PageParam param, out int total)
{
//创建Book的集合
var books = new List<Book>();
for (int i = 0; i < 30; i++)
{
books.Add(new Book()
{
ItemId = "EST-" + i,
ProductId = "AV-SB-" + i,
ListPrice = (i + 5) * 10m,
UnitCost = (i + 2) * 10m,
Attr1 = "Attr" + i,
Status = (short)0
});
}
total = books.Count();
var result = books.OrderBy(b => b.ItemId)
.Skip(param.PageSize*(param.PageIndex - 1))
.Take(param.PageSize);
return result;
}
}
}
Controller有显示页面和响应前台datagrid请求的Action方法
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using DataGridInMvc.Helper;
using DataGridInMvc.Models;
namespace DataGridInMvc.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
//接收datagrid传来的参数
int pageIndex = int.Parse(Request["page"]);
int pageSize = int.Parse(Request["rows"]);
//构建得到分页数据方法所需的参数
var temp = new PageParam()
{
PageIndex = pageIndex,
PageSize = pageSize
};
//分页数据方法的输出参数
int totalNum = 0;
var service = new BookService();
var books = service.LoadPageBookData(temp, out totalNum);
var result = from book in books
select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1};
//total,rows是前台datagrid所需要的
var jsonResult = new {total = totalNum, rows = result};
//把json对象序列化成字符串
string str = JsonSerializeHelper.SerializeToJson(jsonResult);
return Content(str);
}
}
}
□ 这里需要把json对象序列化成string,使用Newtonsoft组件是不错的选择。把序列化和反序列化封装成类。
using Newtonsoft.Json;
namespace DataGridInMvc.Helper
{
public static class JsonSerializeHelper
{
/// <summary>
/// 把object对象序列化成json字符串
/// </summary>
/// <param name="obj">序列话的实例</param>
/// <returns>序列化json字符串</returns>
public static string SerializeToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// 把json字符串反序列化成Object对象
/// </summary>
/// <param name="json">json字符串</param>
/// <returns>对象实例</returns>
public static Object DeserializeFromJson(string json)
{
return JsonConvert.DeserializeObject(json);
}
/// <summary>
/// 把json字符串反序列化成泛型T
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>泛型T</returns>
public static T DeserializeFromJson<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}
视图
※ datagrid的基本属性和方法
※ datagrid分页在前后台的实现
最终效果:
与视图显示对应的view model
public string ItemId { get; set; }
public string ProductId { get; set; }
public decimal ListPrice { get; set; }
public decimal UnitCost { get; set; }
public string Attr1 { get; set; }
public Int16 Status { get; set; }
模拟一个从数据库拿数据,并考虑分页的服务层方法
□ 与分页有关的类
public int PageSize { get; set; }
public int PageIndex { get; set; }
在实际项目中,可以把以上作为一个基类,把各个领域的各种搜索条件封装成继承PageParam的子类。
□ 分页服务层方法
using DataGridInMvc.Models;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities;
namespace DataGridInMvc.Helper
{
public class BookService
{
public IEnumerable<Book> LoadPageBookData(PageParam param, out int total)
{
//创建Book的集合
var books = new List<Book>();
for (int i = 0; i < 30; i++)
{
books.Add(new Book()
{
ItemId = "EST-" + i,
ProductId = "AV-SB-" + i,
ListPrice = (i + 5) * 10m,
UnitCost = (i + 2) * 10m,
Attr1 = "Attr" + i,
Status = (short)0
});
}
total = books.Count();
var result = books.OrderBy(b => b.ItemId)
.Skip(param.PageSize*(param.PageIndex - 1))
.Take(param.PageSize);
return result;
}
}
}
Controller有显示页面和响应前台datagrid请求的Action方法
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using DataGridInMvc.Helper;
using DataGridInMvc.Models;
namespace DataGridInMvc.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
//接收datagrid传来的参数
int pageIndex = int.Parse(Request["page"]);
int pageSize = int.Parse(Request["rows"]);
//构建得到分页数据方法所需的参数
var temp = new PageParam()
{
PageIndex = pageIndex,
PageSize = pageSize
};
//分页数据方法的输出参数
int totalNum = 0;
var service = new BookService();
var books = service.LoadPageBookData(temp, out totalNum);
var result = from book in books
select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1};
//total,rows是前台datagrid所需要的
var jsonResult = new {total = totalNum, rows = result};
//把json对象序列化成字符串
string str = JsonSerializeHelper.SerializeToJson(jsonResult);
return Content(str);
}
}
}
□ 这里需要把json对象序列化成string,使用Newtonsoft组件是不错的选择。把序列化和反序列化封装成类。
using Newtonsoft.Json;
namespace DataGridInMvc.Helper
{
public static class JsonSerializeHelper
{
/// <summary>
/// 把object对象序列化成json字符串
/// </summary>
/// <param name="obj">序列话的实例</param>
/// <returns>序列化json字符串</returns>
public static string SerializeToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// 把json字符串反序列化成Object对象
/// </summary>
/// <param name="json">json字符串</param>
/// <returns>对象实例</returns>
public static Object DeserializeFromJson(string json)
{
return JsonConvert.DeserializeObject(json);
}
/// <summary>
/// 把json字符串反序列化成泛型T
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>泛型T</returns>
public static T DeserializeFromJson<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}
视图