经过一段时间的学习,今天给大家写了一个小案例吧
使用MVC中的VS模板代码块,加上三层架构的代码,实现今天这个小案例
废话少说直接看今天的代码分享
先看看项目展示吧,不喜欢就到此为此,时间很宝贵的
全部房屋信息的展示
添加
(修改)
首先是项目目录(项目名称比较low,就这样吧,博主懒得改)
需要明白的是各个层之间的关系分为(数据访问层,业务逻辑层,表示层)
注意在使用VS模型代码块的时候要在模型上动点手脚
[Display] 特性:在页面显示时的名称
注意: MVC是表示层
在实现房屋发布的时候应该让用户登录(注册)
登录(Userservice中应该只写增删改查,但是博主为了偷个懒就直接写在这里了,注意Service中只写增删改查的基础操作,剩下的业务处理,交给业务逻辑层即可)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZuFang.Models;
using System.Data.SqlClient;
using System.Data;
namespace ZuFang.DAL
{
public class UserService
{
//返回数据库中所有用户信息
public List<User> GetList(string Where,SqlParameter[]sp=null)
{
string sql = "select * from Hos_User Where 1=1 ";
if (!string.IsNullOrEmpty(Where))
{
sql += Where;
}
List<User> List = new List<User>();
try
{
using (var Result = SQLHelper.ExecuteReader(sql, sp))
{
while (Result.Read())
{
List.Add(new User() { UName = Result["UName"].ToString(), UPassWord = Result["UPassWord"].ToString() });
}
}
return List;
}
catch (Exception)
{
throw;
}
}
//实现用户的添加
public bool AddUser(User user)
{
string sql = "insert into Hos_User values(@Name,@Pwd)";
SqlParameter[] Sp =
{
new SqlParameter("@Name",user.UName),
new SqlParameter("@Pwd",user.UPassWord)
};
try
{
return SQLHelper.ExecuteNonQuery(sql, Sp) > 0;
}
catch (Exception)
{
throw;
}
}
//判断是否登录成功
public bool GetUser(User user)
{
string sql = "select count(*) from Hos_User Where UName=@Name and UPassword=@Pwd";
SqlParameter[] Sp =
{
new SqlParameter("@Name",user.UName),
new SqlParameter("@Pwd",user.UPassWord)
};
try
{
return (int)SQLHelper.ExecuteScalar(sql, Sp)>0;
}
catch (Exception)
{
throw;
}
}
}
}
登录(UserManage)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZuFang.DAL;
using ZuFang.Models;
using System.Data;
using System.Data.SqlClient;
namespace ZuFang.BLL
{
public class UserManage
{
UserService UserSer = new UserService();
//返回用户登录是否成功
public User UserCheck(User user)
{
string Where = " and UName=@Name and UPassWord=@Pwd";
SqlParameter[] Sp =
{
new SqlParameter("@Name",user.UName),
new SqlParameter("@Pwd",user.UPassWord)
};
try
{
return UserSer.GetList(Where, Sp)[0];
}
catch (Exception)
{
throw;
}
}
//偷个懒
public bool AddUser(User user)
{
try
{
return UserSer.AddUser(user);
}
catch (Exception)
{
throw;
}
}
//调用数据访问层的方法直接返回即可
public bool GetUser(User user)
{
try
{
return UserSer.GetUser(user);
}
catch (Exception)
{
throw;
}
}
}
}
登录(表示层)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZuFang.BLL;
using ZuFang.Models;
namespace ZuFang.UI.MVC.Controllers
{
public class UserController : Controller
{
UserManage UserMan = new UserManage();
// GET: User
public ActionResult Index()
{
return View();
}
//登录Get请求,作用返回视图
[HttpGet]
public ActionResult CheckUser()
{
return View();
}
//登录Post请求 ,作用处理表单提交方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CheckUser(User user)
{
//调用方法
if (UserMan.GetUser(user))
{
//实现页面跳转
return Redirect("/Goods/GetGoods");
}
//进行页面提示
ModelState.AddModelError("","账号密码不匹配!");
return View();
}
//和上面一样
[HttpGet]
public ActionResult InsertUser()
{
return View();
}
[HttpPost]
public ActionResult InsertUser(User user)
{
if (UserMan.AddUser(user))
{
return RedirectToAction("CheckUser");
}
return View();
}
}
}
使用VS的视图模板块(Create)
@model ZuFang.Models.User
@{
ViewBag.Title = "CheckUser";
}
<h2>CheckUser</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UPassWord, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UPassWord, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UPassWord, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="登录" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@*第二个参数代表,当前控制器中的action方法名,默认当前控制器*@
@Html.ActionLink("注册", "InsertUser")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
注册(cshtml页面)
@model ZuFang.Models.User
@{
ViewBag.Title = "InsertUser";
}
<h2>InsertUser</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UPassWord, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UPassWord, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UPassWord, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="注册" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("登录", "CheckUser")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
房屋管理功能(数据访问层)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZuFang.Models;
using System.Data;
using System.Data.SqlClient;
namespace ZuFang.DAL
{
public class HouseService
{
/// <summary>
/// 返回全部的房屋信息
/// </summary>
/// <param name="Where">业务逻辑层传递过来的数据</param>
/// <param name="Sp">数组参数</param>
/// <returns>全部信息</returns>
public List<House> GetList(string Where, SqlParameter[] Sp = null)
{
string sql = "select * from Goods where 1=1 ";
if (!string.IsNullOrEmpty(Where))
{
sql += Where;
}
List<House> List = new List<House>();
try
{
using (var Result = SQLHelper.ExecuteReader(sql, Sp))
{
while (Result.Read())
{
House house = new House();
house.HuId = Result["HuId"].ToString();
house.MianJi = Convert.ToInt32(Result["MianJi"]);
house.Money = Convert.ToDouble(Result["Money"]);
house.Name = Result["Name"].ToString();
house.Phone = Result["Phone"].ToString();
house.XiangJie = Result["XiangJie"].ToString();
List.Add(house);
}
}
return List;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 实现房屋信息的添加
/// </summary>
/// <param name="house">对象参数</param>
/// <returns>是否成功</returns>
public bool InsertGood(House house)
{
//[Name], [HuId], [MianJi], [Phone], [XiangJie], [Money]
string sql = "insert into Goods Values(@Name,@HuId,@MianJi,@Phone,@XiangJie,@Money)";
SqlParameter[] Sp =
{
new SqlParameter("@Name",house.Name),
new SqlParameter("@HuId",house.HuId),
new SqlParameter("@MianJi",house.MianJi),
new SqlParameter("@Phone",house.Phone),
new SqlParameter("@XiangJie",house.XiangJie),
new SqlParameter("@Money",house.Money)
};
try
{
return SQLHelper.ExecuteNonQuery(sql, Sp) > 0;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 实现房屋信息的修改
/// </summary>
/// <param name="house">对象参数</param>
/// <returns>是否成功</returns>
public bool UpdateGood(House house)
{
string sql = "update Goods Set HuId=@HuId,MianJi=@MianJi,Phone=@Phone,XiangJie=@XiangJie,[Money]=@Money Where Name=@Name";
SqlParameter[] Sp =
{
new SqlParameter("@Name",house.Name),
new SqlParameter("@HuId",house.HuId),
new SqlParameter("@MianJi",house.MianJi),
new SqlParameter("@Phone",house.Phone),
new SqlParameter("@XiangJie",house.XiangJie),
new SqlParameter("@Money",house.Money),
};
try
{
return SQLHelper.ExecuteNonQuery(sql, Sp) > 0;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 通过name实现删除
/// </summary>
/// <param name="Name">name参数</param>
/// <returns>是否成功</returns>
public bool DeleteGood(string Name)
{
string sql = "delete from Goods Where Name=@Name";
SqlParameter[] Sp =
{
new SqlParameter("@Name",Name)
};
try
{
return SQLHelper.ExecuteNonQuery(sql, Sp) > 0;
}
catch (Exception)
{
throw;
}
}
}
}
房屋管理(业务逻辑层)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZuFang.DAL;
using ZuFang.Models;
using System.Data;
using System.Data.SqlClient;
namespace ZuFang.BLL
{
public class HouseManage
{
HouseService HouseSer = new HouseService();
//对应数据访问层的方法
public List<House> GetList()
{
try
{
return HouseSer.GetList(null);
}
catch (Exception)
{
throw;
}
}
public House GetNamebyGood(string name)
{
string Where = " and name=@name";
SqlParameter[] Sp =
{
new SqlParameter("@Name",name)
};
try
{
return HouseSer.GetList(Where, Sp)[0];
}
catch (Exception)
{
throw;
}
}
public bool AddGood(House house)
{
try
{
return HouseSer.InsertGood(house);
}
catch (Exception)
{
throw;
}
}
public bool UpdateGood(House house)
{
try
{
return HouseSer.UpdateGood(house);
}
catch (Exception)
{
throw;
}
}
public bool DeleteGood(string Name)
{
try
{
return HouseSer.DeleteGood(Name);
}
catch (Exception)
{
throw;
}
}
}
}
房屋管理(UI)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZuFang.BLL;
using ZuFang.Models;
namespace ZuFang.UI.MVC.Controllers
{
public class GoodsController : Controller
{
HuXingManage HuXingMan = new HuXingManage();
HouseManage HouseMan = new HouseManage();
// GET: Goods
public ActionResult Index()
{
return View();
}
//显示全部的Goods
public ActionResult GetGoods()
{
var Item = HouseMan.GetList();
foreach (var item in Item)
{
item.HuId = HuXingMan.GetNameById(Convert.ToInt32(item.HuId));
}
return View(Item);
}
[HttpGet]
public ActionResult Update()
{
var Item = RouteData.Values["Id"].ToString();
return View(HouseMan.GetNamebyGood(Item));
}
/// <summary>
/// 实现修改
/// </summary>
/// <param name="house">对象参数</param>
/// <returns>返回动作方法</returns>
[HttpPost]
public ActionResult Update(House house)
{
if(HouseMan.UpdateGood(house))
{
return RedirectToAction("GetGoods");
}
return View();
}
/// <summary>
/// 实现删除,删除后的刷新
/// </summary>
/// <returns>视图</returns>
public ActionResult DeleteGood()
{
HouseMan.DeleteGood(RouteData.Values["id"].ToString());
return View("GetGoods", HouseMan.GetList());
}
//显示页面
[HttpGet]
public ActionResult CreateGood()
{
return View();
}
/// <summary>
/// 实现房屋添加
/// </summary>
/// <param name="house">对象参数</param>
/// <returns>跳转的GetGoods Action方法上</returns>
[HttpPost]
public ActionResult CreateGood(House house)
{
//house.HuId =HuXingMan.GetIdByName(house.HuId).ToString();
HouseMan.AddGood(house);
return RedirectToAction("GetGoods");
}
}
}
还有视图和 没有写过来,使用VS模型代码块,使用就可以了(代码段中有注释,看看便于理解)
如果需要代码私信进行(给数据库)
结尾: 不知不觉一月份已经过半,时间不等人,凡事贵在坚持,加油