您的位置:首页 > 其它

datagrid在MVC中的运用02-结合搜索

2014-02-28 13:04 295 查看
本文接着上一篇,来体验给datagrid加上搜索功能。主要涉及到:

※ 把一个div与datagrid相关起来
※ datagrid接收查询参数
※ 查询参数的封装

效果图:

展开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"]);

//接收搜索参数
string itemId = Request["ItemId"];
string productId = Request["ProductId"];

//构建得到分页数据方法所需的参数
var temp = new BookParam()
{
PageIndex = pageIndex,
PageSize = pageSize,
ItemId = itemId,
ProductId = productId
};

//分页数据方法的输出参数
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);
}
}
}


JsonSerializeHelper静态类在上一篇介绍过。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: