这里MVC中用到了反射,工厂,泛型,接口
在搭建框架的时候,除了MVC的三层以外,还有泛型的接口层和工厂层
下面是dal层调用sql存储过程,增删改查,dal层继承了接口层,实现了接口层里面的方法
1 namespace DAL 2 { 3 public class DalHouse : IHouse 4 { 5 public int Add(HouseInfo m) 6 { 7 string sql = "pro_add"; 8 SqlParameter eid = new SqlParameter("@eid", m.Eid); 9 SqlParameter bid = new SqlParameter("@bid", m.Bid); 10 SqlParameter floornum = new SqlParameter("@floornum", m.FloorNum); 11 SqlParameter roomnum = new SqlParameter("@roomnum", m.RoomNum); 12 SqlParameter spaces = new SqlParameter("@spaces", m.Spaces); 13 SqlParameter ads = new SqlParameter("@ads", m.Addresses); 14 SqlParameter tid = new SqlParameter("@tid", m.Tid); 15 SqlParameter sids = new SqlParameter("@sids", m.Sids); 16 SqlParameter uid = new SqlParameter("@uid", m.Uid); 17 SqlParameter[] paras = new SqlParameter[] { eid, bid, floornum, roomnum, spaces, ads, tid, sids, uid }; 18 int i = DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, sql, paras); 19 return i; 20 } 21 22 public int Delete(string id) 23 { 24 string sql = "pro_delete"; 25 SqlParameter pid = new SqlParameter("@id", id); 26 SqlParameter[] paras = new SqlParameter[] { pid }; 27 int i = DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, sql, paras); 28 return i; 29 } 30 31 public HouseInfo SelectById(int id) 32 { 33 string sql = "pro_selectbyid"; 34 SqlParameter pid = new SqlParameter("@id",id); 35 SqlParameter[] paras = new SqlParameter[] { pid}; 36 DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras); 37 List<HouseInfo> info = JsonConvert.DeserializeObject<List<HouseInfo>>(JsonConvert.SerializeObject(dt)); 38 return info[0]; 39 } 40 41 public PageList ShowAll(DataModel d) 42 { 43 string sql = "pro_ShowAll"; 44 SqlParameter paraEid = new SqlParameter("@Estateid", d.Eid); 45 SqlParameter paraBid = new SqlParameter("@BuildingId", d.Bid); 46 SqlParameter paraTid = new SqlParameter("@Type", d.Tid); 47 SqlParameter paraSid = new SqlParameter("@State", d.Sids); 48 SqlParameter paraAddress = new SqlParameter("@Address", d.Addresses); 49 SqlParameter paraSize = new SqlParameter("@size", d.Size); 50 SqlParameter paraIndex = new SqlParameter("@index", d.Index); 51 SqlParameter count = new SqlParameter("@totalCount", SqlDbType.Int); 52 count.Direction = ParameterDirection.Output; 53 SqlParameter page = new SqlParameter("@totalPage", SqlDbType.Int); 54 page.Direction = ParameterDirection.Output; 55 SqlParameter[] paras = new SqlParameter[] { paraEid, paraBid, paraTid, paraSid, paraAddress, paraSize, count, paraIndex, page }; 56 DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras); 57 PageList p = new PageList(); 58 p.TotalCount = Convert.ToInt32(count.Value); 59 p.TotalPage = Convert.ToInt32(page.Value); 60 p.House = JsonConvert.DeserializeObject<List<HouseInfo>>(JsonConvert.SerializeObject(dt)); 61 return p; 62 } 63 64 public List<Building> ShowBuilding() 65 { 66 string sql = "select * from Building"; 67 DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.Text,sql); 68 return JsonConvert.DeserializeObject<List<Building>>(JsonConvert.SerializeObject(dt)); 69 70 } 71 72 public List<BuildStruct> ShowBuildStruct() 73 { 74 string sql = "select * from BuildStruct"; 75 DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql); 76 return JsonConvert.DeserializeObject<List<BuildStruct>>(JsonConvert.SerializeObject(dt)); 77 } 78 79 public List<HouseEatate> ShowEstate() 80 { 81 string sql = "select * from HouseEstate"; 82 DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql); 83 return JsonConvert.DeserializeObject<List<HouseEatate>>(JsonConvert.SerializeObject(dt)); 84 } 85 86 public List<States> ShowStates() 87 { 88 string sql = "select * from States"; 89 DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql); 90 return JsonConvert.DeserializeObject<List<States>>(JsonConvert.SerializeObject(dt)); 91 } 92 93 public List<HouseType> ShowType() 94 { 95 string sql = "select * from HouseType"; 96 DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql); 97 return JsonConvert.DeserializeObject<List<HouseType>>(JsonConvert.SerializeObject(dt)); 98 } 99 100 public int Update(HouseInfo m) 101 { 102 string sql = "pro_update"; 103 SqlParameter id = new SqlParameter("@id", m.Id); 104 SqlParameter eid = new SqlParameter("@eid",m.Eid); 105 SqlParameter bid = new SqlParameter("@bid", m.Bid); 106 SqlParameter floornum = new SqlParameter("@floornum", m.FloorNum); 107 SqlParameter roomnum = new SqlParameter("@roomnum", m.RoomNum); 108 SqlParameter spaces = new SqlParameter("@spaces", m.Spaces); 109 SqlParameter ads = new SqlParameter("@ads", m.Addresses); 110 SqlParameter tid = new SqlParameter("@tid", m.Tid); 111 SqlParameter sids = new SqlParameter("@sids", m.Sids); 112 SqlParameter uid = new SqlParameter("@uid", m.Uid); 113 SqlParameter[] paras = new SqlParameter[] {id,eid,bid,floornum,roomnum,spaces,ads,tid,sids,uid }; 114 int i= DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras); 115 return i; 116 117 } 118 } 119 }
这是controller与前台交互,控制器里面的方法需要注意的是下拉的绑定,还有分部视图的使用
我用的是强类型视图,所以视图页面和控制器里面参数的变量名要一致,否则控制器中的方法接受不到值的
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using DAL; using IDAL; using Model; using Factory; using BLL; namespace EF_House.Controllers { public class HouseController : Controller { // GET: House public ActionResult Index(int index=1,int size=3) { DataModel d = new DataModel(); d.Bid = 0; d.Eid = 0; d.Sids = 0; d.Tid = 0; d.Addresses = ""; d.Index = index; d.Size = size; BllHouse bll = new BllHouse(); PageList info = bll.ShowAll(d); ViewBag.totalpage = info.TotalPage; ViewBag.totalcount = info.TotalCount; ViewBag.index = d.Index; //下拉绑定 ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename"); ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName"); ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname"); ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname"); return View(info.House); } public ActionResult Search(string Addresses="",int Bid=0,int Eid=0,int Sids=0,int Tid=0,int index=1,int size=3) { if (Addresses == null) { Addresses = ""; } ViewBag.bid = Bid; ViewBag.eid = Eid; ViewBag.sids = Sids; ViewBag.tid = Tid; ViewBag.ads = Addresses; DataModel d = new DataModel(); d.Bid = Bid; d.Eid = Eid; d.Sids = Sids; d.Tid = Tid; d.Addresses = Addresses; d.Index = index; d.Size = size; BllHouse bll = new BllHouse(); PageList p = bll.ShowAll(d); ViewBag.totalcount = p.TotalCount; ViewBag.totalpage = p.TotalPage; ViewBag.index = d.Index; //下拉绑定 ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename"); ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName"); ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname"); ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname"); return PartialView("Search",p.House); } public int DeleteAll(string id) { BllHouse bll = new BllHouse(); int i = bll.Delete(id); return i; } public void Delete(string id) { BllHouse bll = new BllHouse(); int i = bll.Delete(id); if (i > 0) { Response.Write("<script>alert(‘删除成功‘);location.href=‘/house/index/1‘</script>"); } else { Response.Write("<script>alert(‘删除失败‘)</script>"); } } public ActionResult Update(int id) { BllHouse bll = new BllHouse(); ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename"); ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName"); ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname"); ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname"); ViewBag.Struct = new SelectList(bll.ShowBuildStruct(), "Id", "Uname"); HouseInfo h = bll.SelectById(id); return View(h); } [HttpPost] public void Update(HouseInfo h) { BllHouse bll = new BllHouse(); int i = bll.Update(h); if (i > 0) { Response.Write("<script>alert(‘修改成功‘);location.href=‘/house/index/1‘</script>"); } else { Response.Write("<script>alert(‘修改失败‘)</script>"); } } public ActionResult Add() { BllHouse bll = new BllHouse(); ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename"); ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName"); ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname"); ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname"); ViewBag.Struct = new SelectList(bll.ShowBuildStruct(), "Id", "Uname"); return View(); } [HttpPost] public ActionResult Add(HouseInfo h) { BllHouse bll = new BllHouse(); int i = bll.Add(h); if (i > 0) { return Content("<script>alert(‘添加成功‘);location.href=‘/house/index/1‘</script>"); } else { return Content("<script>alert(‘添加失败‘)</script>"); } } } }
这里需要说一下,jquery写的全选和批删的方法,详情看代码
<script> //全选 $(function () { $("#CheckAll").click(function () { if (this.checked) { $("input[name=‘check‘]").each(function () { this.checked = true; }) } else { $("input[name=‘check‘]:checked").each(function () { this.checked = false; }) } }) }) function DelAll() { var check = []; $("input[name=‘check‘]:checked").each(function () { check.push($(this).val()); }) if (check == 0) { alert("请先进行选择"); } else { $.ajax({ type: "post", url: "/house/deleteall", data: { id: check.toString() }, success: function (a) { if (a > 0) { alert("批量删除成功!"); location.reload(); } } }) } } </script>
基本的页面布局,这里就不展示了,
还需要说的就是使用强类型视图,在修改的时候,
需要使用@html.Hidden("Id")
原文地址:https://www.cnblogs.com/dxx117/p/9525636.html
时间: 2024-10-29 04:39:32