当前位置:首页 > 企业简介 >

.NET架构MVC5编程实现简易购物网站

.NET架构MVC5编程实现简易购物网站

1.首先先创建一个数据模型类Product;

\

namespace MVCGW.Models { public class Product { public int ID { get; set; } [Display(Name = "商品名称")] [Required(ErrorMessage = "必填")] [StringLength(60, MinimumLength = 3, ErrorMessage = "必须是[3,60]个字符")] public string Title { get; set; } [Display(Name = "商品类型")] [Required] public string Genre { get; set; } [Display(Name = "商品简介")] [Required] public string Description { get; set; } [Display(Name = "商品售价")] [Range(1, 10000)] [DataType(DataType.Currency)] public decimal Price { get; set; } [Display(Name = "上新日期")] [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } [Display(Name = "商品图片")] [Required] public string Photo { get; set; } }

还有ShoppingCar(购物车)类和Order(订单)类,

为了在数据库中保存图片,保存的是图片路径;

2.创建数据库上下文类;

public class ProductDBContext : DbContext { public DbSet<Product> Products { get; set; } public DbSet<ShoppingCar> Shoppingcars { get; set; } public DbSet<Order> Orders { get; set; } }

3.创建数据库连接串;

//Web.config文件: <connectionStrings> <add name="ProductDBContext" connectionString="Data Source=(LocalDb)\v11.0; AttachDbFilename=|DataDirectory|\Products.mdf; AttachDbFilename=|DataDirectory|\Shoppingcars.mdf; AttachDbFilename=|DataDirectory|\Orders.mdf; Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

4。添加视图

笔者的网站使用了layout布局,这样代码利用率高

@model IEnumerable<MVCGW.Models.Product> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <style> .big { margin: 0px auto 0px auto; width: 940px; height: 516px; /* background-image: url('/images/tjsp_border.jpg'); /*推荐商品背景图片*/ padding: 45px 20px 21px 20px; /*背景图片框有一定的厚度*/ } .flower { width: 225px; height: 235px; float: left; /*图片并排*/ padding: 12px; /*border:1px #CCCCCC solid;text-align:center;*/ overflow: hidden; } .flower_desc { height: 25px; line-height: 30px; text-align: center; font-size: 12px; } </style> <div> @using (Html.BeginForm("Index", "Product", FormMethod.Get)) { <p> 宝贝类型:@Html.DropDownList("bbGenre", "all") <input type="submit" value="查询" /> </p> } </div> <div class="big"> @foreach (var m in Model) { <div class="flower"> <div><img src=http://www.2cto.com/kf/201612/"/@m.Photo"width="170" height="160" /></div> <div class="flower_desc">@m.Title.Trim() &nbsp;&nbsp;@m.Price 元 </div><br /> @Html.ActionLink("加入购物车", "PutCar", new { id = m.ID }) | @Html.ActionLink("详情", "Details", new { id = m.ID }) </div> } </div>

可使用应用list模板

5.创建控制器Productcontroller,实现实现业务逻辑

编写index,

public ActionResult Index(string bbGenre) { var GenreLst = new List<string>();//实现按宝贝类型查询,控制器传递数据给Select的方法 var GenreQry = from d in db.Products orderby d.Genre select d.Genre; GenreLst.AddRange(GenreQry.Distinct()); //去重 ViewBag.bbGenre = new SelectList(GenreLst);//设置的ViewBag属性值一定要与Select的name保持一致 var rs1 = from m in db.Products select m; if (!String.IsNullOrEmpty(bbGenre)) { rs1 = rs1.Where(x => x.Genre == bbGenre); return View(rs1); } var rs = from m in db.Products where new int[] { 1,2,3,4,5,6,7,8 }.Contains(m.ID) select m; return View(rs); //向视图传递的数据是对象rs /*ViewData.Model = rs; return View();*/ } public ActionResult PutCar(int id) { //会员登录后才能购物 if (Session["Username"] == null) return RedirectToAction("Login", "Product"); var rec1 = db.Products.Find(id); //默认数量sl=1,在生成订单前可以修改 ShoppingCar rec2 = new ShoppingCar { username = (string)Session["username"], Title = rec1.Title, Price = rec1.Price,Genre=rec1.Genre, Photo = rec1.Photo, num = 1 }; db.Shoppingcars.Add(rec2); db.SaveChanges(); return RedirectToAction("Index"); } public ActionResult ShoppingCar() { decimal i = 0; var sc = from m in db.Shoppingcars select m; if (Session["Username"] != null) { string name = (string)Session["Username"]; sc = sc.Where(s => s.username.Contains(name)); foreach (var item in sc) { i += item.Price; } ViewBag.Data = i;//后台算出总价传值给前台视图 return View(sc); } else return RedirectToAction("Login", "Product"); } public ActionResult Besure()//实现确认购买生成订单,向订单类中添加已购买商品数据,购物车清空 { var sc = from m in db.Shoppingcars select m; if (Session["Username"] != null) { string name = (string)Session["Username"]; sc = sc.Where(s => s.username==name) ; foreach (var rec1 in sc) { Order rec2 = new Order { username = (string)Session["username"], Title = rec1.Title, Price = rec1.Price, Genre = rec1.Genre, Photo = rec1.Photo, num = 1 }; db.Orders.Add(rec2); } foreach (var rec3 in sc) { db.Shoppingcars.Remove(rec3); } db.SaveChanges(); return RedirectToAction("Order"); } else return RedirectToAction("Login", "Product"); } public ActionResult Order() { decimal i = 0; var sc = from m in db.Orders select m; if (Session["Username"] != null) { string name = (string)Session["Username"]; sc = sc.Where(s => s.username.Contains(name)); foreach (var item in sc) { i += item.Price; } ViewBag.Data1 = i; return View(sc); } else return RedirectToAction("Login", "Product"); }

\

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:鄂州SEO http://ezhou.raoyu.net

上一篇:DIV水平和垂直居中问题

下一篇:最后一页