Asp.Net MVC结合Linq to SQL实现CRUD


ICategoryService.cs

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");
        }
    }
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值