Asp.Net MVC结合Linq to SQL实现CRUD
2013-01-16 21:59
716 查看
ICategoryService.cs
MockCategoryService.cs
控制层:
CategoryController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookShopMVC.Models.Interfaces;
using BookShopMVC.Models.MockModels;
using Webdiyer.WebControls.Mvc;
using BookShopMVC.Models;
namespace BookShopMVC.Controllers
{
public class CategoryController : Controller
{
ICategoryService service = new MockCategoryService();
public ActionResult Index(int? id)
{
PagedList<Category> pages = service.CategoryListPgae(id);
return View(pages);
}
public ActionResult Add()
{
return View("AddCategory");
}
public ActionResult AddCategory(Category c)
{
service.Add(c);
return RedirectToAction("Index");
}
public ActionResult Delete(int id)
{
service.Delete(id);
return RedirectToAction("Index");
}
public ActionResult Edit(int id)
{
Category c = service.GetCategory(id);
return View("EditCategory",c);
}
public ActionResult EditCategory(Category c)
{
service.Edit(c);
return RedirectToAction("Index");
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Webdiyer.WebControls.Mvc; namespace BookShopMVC.Models.Interfaces { public interface ICategoryService { PagedList<Category> CategoryListPgae(int? id); void Add(Category c); void Delete(int id); void Edit(Category c); Category GetCategory(int id); } }
MockCategoryService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using BookShopMVC.Models.Interfaces; using Webdiyer.WebControls.Mvc; namespace BookShopMVC.Models.MockModels { public class MockCategoryService : ICategoryService { //默认分页数 private const int defaultPageSize = 10; //使用MvcPager控件进行分页 public Webdiyer.WebControls.Mvc.PagedList<Category> CategoryListPgae(int? id) { using (var db = new DataRepositoryDataContext()) { PagedList<Category> data = db.Category.ToPagedList(id ?? 1, defaultPageSize); return data; } } public void Add(Category c) { using (var db = new DataRepositoryDataContext()) { db.Category.InsertOnSubmit(c); db.SubmitChanges(); } } public void Delete(int id) { using (var db = new DataRepositoryDataContext()) { Category c1 = db.Category.First<Category>(item => item.CategoryId == id); db.Category.DeleteOnSubmit(c1); db.SubmitChanges(); } } public void Edit(Category c) { using (var db = new DataRepositoryDataContext()) { Category c1 = db.Category.First<Category>(item => item.CategoryId == c.CategoryId); c1.CategoryName = c.CategoryName; db.SubmitChanges(); } } public Category GetCategory(int id) { using (var db = new DataRepositoryDataContext()) { return db.Category.Single(c => c.CategoryId == id); } } } }
控制层:
CategoryController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookShopMVC.Models.Interfaces;
using BookShopMVC.Models.MockModels;
using Webdiyer.WebControls.Mvc;
using BookShopMVC.Models;
namespace BookShopMVC.Controllers
{
public class CategoryController : Controller
{
ICategoryService service = new MockCategoryService();
public ActionResult Index(int? id)
{
PagedList<Category> pages = service.CategoryListPgae(id);
return View(pages);
}
public ActionResult Add()
{
return View("AddCategory");
}
public ActionResult AddCategory(Category c)
{
service.Add(c);
return RedirectToAction("Index");
}
public ActionResult Delete(int id)
{
service.Delete(id);
return RedirectToAction("Index");
}
public ActionResult Edit(int id)
{
Category c = service.GetCategory(id);
return View("EditCategory",c);
}
public ActionResult EditCategory(Category c)
{
service.Edit(c);
return RedirectToAction("Index");
}
}
}
相关文章推荐
- ADO.NET与ORM的比较(3)Linq to SQL实现CRUD
- asp.net mvc 遍历linq to sql 多表联查
- ASP.NET使用Linq to SQL实现基本的增、删、改、查及绑定控件
- 【转载】ADO.NET与ORM的比较(3):Linq to SQL实现CRUD
- 【译】Asp.net MVC并不仅仅只是Linq to SQL
- (转)【译】Asp.net MVC并不仅仅只是Linq to SQL
- 在asp.net mvc中创建使用Linq to sql的分页控件[转]
- 【译】Asp.net MVC并不仅仅只是Linq to SQL
- ADO.NET与ORM的比较(3):Linq to SQL实现CRUD
- Microsoft ASP.NET MVC Preview 5 及LINQ TO SQL最新版开发示例演示(提供源码下载)
- Asp.net MVC 模型(Entity Framework、LinqToSql、显示数据库数据表格)
- Microsoft ASP.NET MVC Preview 5 及LINQ TO SQL最新版开发示例演示(提供源码下载)
- Asp.Net MVC 模型(使用LINQ to SQL创建Model类)2
- 一起谈.NET技术,【译】ASP.NET MVC并不仅仅只是Linq to SQL
- ADO.NET与ORM的比较(3)Linq to SQL实现CRUD
- Asp.Net MVC 模型(使用LINQ to SQL创建Model类) – Part.2
- Microsoft ASP.NET MVC Preview 5 及LINQ TO SQL最新版开发示例演示(提供源码下载)
- ADO.NET与ORM的比较Linq to SQL实现CRUD
- 在asp.net mvc中创建使用Linq to sql的分页控件
- Microsoft ASP.NET MVC Preview 5 及LINQ TO SQL最新版开发示例演示(提供源码下载)