[转载]asp.net mvc 2 简简单单做开发 自定义Controller基类 – 飞创cms – 博客园.
以前使用ASP.NET时总是自定义一个基类来继承Page类,来实现一些页面中常用的操作来减少代码。ASP.NET mvc 2 里没有分页功能,就在基类里实现分页的功能吧,在页面中定义分页常用的变量,如 RecordCount、PageSize、CurPage等。通过GetPager() 把分页字符串赋给ViewData[“Page”],view页面中直接输出ViewData[“page”]。
实例代码 BaseController.cs
1 public class BaseController : Controller
2 {
3 //linq数据操作类db
4 public DB db = new DB();
5 public int RecordCount = 0;
6 public int PageSize = 10;
7
8 public int CurPage
9 {
10 get
11 {
12 return DNTRequest.GetInt(“page“, 1);
13 }
14 }
15 public void GetPager()
16 {
17 int PageCount = (int)Math.Ceiling(RecordCount/ ((double)PageSize));
18 ViewData[“Page“] = Utils.GetPageNumbers(CurPage, PageCount, GetUrl(), 5);
19 }
20 public string GetUrl()
21 {
22 string url = Request.Url.ToString();
23 url = url.Substring(url.LastIndexOf(“/“) + 1);
24
25 Regex reg = new Regex(@”&page=([0-9]+)“, RegexOptions.IgnoreCase);
26 Match m = reg.Match(url);
27 if (m.Success)
28 {
29 url = url.Replace(m.Groups[0].Value, “”);
30 }
31 reg = new Regex(@”\?page=([0-9]+)“, RegexOptions.IgnoreCase);
32 m = reg.Match(url);
33 if (m.Success)
34 {
35 url = url.Replace(m.Groups[0].Value, “?1=1“);
36 }
37 return url;
38 }
39 }
2 {
3 //linq数据操作类db
4 public DB db = new DB();
5 public int RecordCount = 0;
6 public int PageSize = 10;
7
8 public int CurPage
9 {
10 get
11 {
12 return DNTRequest.GetInt(“page“, 1);
13 }
14 }
15 public void GetPager()
16 {
17 int PageCount = (int)Math.Ceiling(RecordCount/ ((double)PageSize));
18 ViewData[“Page“] = Utils.GetPageNumbers(CurPage, PageCount, GetUrl(), 5);
19 }
20 public string GetUrl()
21 {
22 string url = Request.Url.ToString();
23 url = url.Substring(url.LastIndexOf(“/“) + 1);
24
25 Regex reg = new Regex(@”&page=([0-9]+)“, RegexOptions.IgnoreCase);
26 Match m = reg.Match(url);
27 if (m.Success)
28 {
29 url = url.Replace(m.Groups[0].Value, “”);
30 }
31 reg = new Regex(@”\?page=([0-9]+)“, RegexOptions.IgnoreCase);
32 m = reg.Match(url);
33 if (m.Success)
34 {
35 url = url.Replace(m.Groups[0].Value, “?1=1“);
36 }
37 return url;
38 }
39 }
ArticleController.cs
代码
public class ArticleController : BaseController
{
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
var cate=db.Category.ToList();
cate.Insert(0,new Category() {Id=0, Name=“未设置“});
ViewData[“category“] = cate;
base.OnResultExecuting(filterContext);
{
var article = db.Find<Article>(art);
var list = db.Article.Where(d =>d.title.Contains(“22“));
RecordCount = article.Count();
article = article.Skip((CurPage – 1) * PageSize).Take(PageSize);
GetPager();
return View(“index“, article);
} //public ActionResult Index()
//{
// ViewData[“searchModel”] = new Article();
// var article = from d in db.Article
// select d;
// RecordCount = article.Count(); // article = article.Skip((CurPage-1)*PageSize).Take(PageSize);
// GetPager();
// return View(“index”,article); //}
public ActionResult Add()
{
Article art = new Article();
art.CreateTime = DateTime.Now;
return View(art);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Add(Article art)
{
try
{
if (this.ModelState.IsValid)
{
db.Article.InsertOnSubmit(art);
db.SubmitChanges();
return RedirectToAction(“index“);
}
else
{
return View(art);
}
}
catch(Exception e)
{
return Content(e.Message);
}
{
return View(db.Article.FirstOrDefault(d=>d.Id==id));
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(int id,Article art1)
{
try
{
Article art=db.Article.FirstOrDefault(d=>d.Id==id);
UpdateModel(art);
db.SubmitChanges();
return this.RedirectToAction(“index“);
}
catch
{
return View(art1);
}
}
public ActionResult Delete(int id)
{
db.Article.DeleteOnSubmit(db.Article.FirstOrDefault(d => d.Id == id));
db.SubmitChanges();
return RedirectToAction(“index“);
}
{
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
var cate=db.Category.ToList();
cate.Insert(0,new Category() {Id=0, Name=“未设置“});
ViewData[“category“] = cate;
base.OnResultExecuting(filterContext);
}
public ActionResult Index(Article art){
ViewData[
“searchModel“] = art;var article = db.Find<Article>(art);
var list = db.Article.Where(d =>d.title.Contains(“22“));
RecordCount = article.Count();
article = article.Skip((CurPage – 1) * PageSize).Take(PageSize);
GetPager();
return View(“index“, article);
} //public ActionResult Index()
//{
// ViewData[“searchModel”] = new Article();
// var article = from d in db.Article
// select d;
// RecordCount = article.Count(); // article = article.Skip((CurPage-1)*PageSize).Take(PageSize);
// GetPager();
// return View(“index”,article); //}
public ActionResult Add()
{
Article art = new Article();
art.CreateTime = DateTime.Now;
return View(art);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Add(Article art)
{
try
{
if (this.ModelState.IsValid)
{
db.Article.InsertOnSubmit(art);
db.SubmitChanges();
return RedirectToAction(“index“);
}
else
{
return View(art);
}
}
catch(Exception e)
{
return Content(e.Message);
}
}
{
return View(db.Article.FirstOrDefault(d=>d.Id==id));
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(int id,Article art1)
{
try
{
Article art=db.Article.FirstOrDefault(d=>d.Id==id);
UpdateModel(art);
db.SubmitChanges();
return this.RedirectToAction(“index“);
}
catch
{
return View(art1);
}
}
public ActionResult Delete(int id)
{
db.Article.DeleteOnSubmit(db.Article.FirstOrDefault(d => d.Id == id));
db.SubmitChanges();
return RedirectToAction(“index“);
}
}
————————————————————————————
——–