1、使用Code First技术创建一个Movie数据模型。
public class Movie { public int ID { get; set; } //电影编号 public string Title { get; set; } //电影名称 public DateTime ReleaseDate { get; set; } //上映时间 public string Genre { get; set; } //电影类型 public decimal Price { get; set; } //电影票价 public string Rating { get; set; } //电影分级 }
2、使用MVC相关技术实现数据的列表显示和新增功能。
3、完成数据的编辑、删除、明细和条件查询等功能。
4、完成如下查询:
(1)查询尚未上映电影的信息
(4)查询票价在某个区间的电影信息
效果
(源码在文章结尾)
主要涉及知识点
1、ASP.NET WEB MVC下的目录结构以及基础编程
2、Linq查询操作
3、Code First
4、各模板View的建立和使用
主要代码
MovieController.cs
using ProjectThree.Models; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; namespace ProjectThree.Controllers { public class MovieController : Controller { MovieDBContext db = new MovieDBContext(); // GET: Movie public ActionResult Index(string movieOn, string movieGenre, string searchString, string lowPrice, string highPrice) { //初始化电影是否上映下拉 var GenreLst1 = new List<string>(); GenreLst1.Add("是"); GenreLst1.Add("否"); ViewBag.movieOn = new SelectList(GenreLst1); //初始化电影类型下拉 var GenreLst2 = new List<string>(); var GenreQry = from d in db.Movies orderby d.Genre select d.Genre; GenreLst2.AddRange(GenreQry.Distinct()); //去重 ViewBag.movieGenre = new SelectList(GenreLst2); var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(movieOn)) { DateTime dtNow = DateTime.Now; if (movieOn.Equals("是")) { movies = movies.Where(s => DateTime.Compare(dtNow, s.ReleaseDate) > 0); } else if (movieOn.Equals("否")) { movies = movies.Where(s => DateTime.Compare(dtNow, s.ReleaseDate) <= 0); } } if (!String.IsNullOrEmpty(movieGenre)) { movies = movies.Where(x => x.Genre == movieGenre); } if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } if ((!String.IsNullOrEmpty(lowPrice)) && (!String.IsNullOrEmpty(highPrice))) { try { Decimal low = Decimal.Parse(lowPrice); Decimal high = Decimal.Parse(highPrice); if (high < low) { Response.Write("<script>alert('左边价格不可大于右边!');</script>"); } else { movies = movies.Where(s => s.Price >= low); movies = movies.Where(s => s.Price <= high); } } catch { Response.Write("<script>alert('必须输入数字!');</script>"); return View(movies); } } return View(movies); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Movie m) { if (ModelState.IsValid) { db.Movies.Add(m); db.SaveChanges(); return RedirectToAction("Index", "Movie"); } return View(m); } public ActionResult Delete(int? id) { Movie m = db.Movies.Find(id); if (m != null) { db.Movies.Remove(m); db.SaveChanges(); } return RedirectToAction("Index", "Movie"); } public ActionResult Edit(int id) { Movie stu = db.Movies.Find(id); return View(stu); } [HttpPost] public ActionResult Edit(Movie stu) { db.Entry(stu).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index", "Movie"); } } }
Movie.cs
using System; using System.ComponentModel.DataAnnotations; namespace ProjectThree.Models { public class Movie { [Display(Name = "电影编号")] public int ID { get; set; } //电影编号 [Display(Name = "电影名称")] [Required(ErrorMessage = "必填")] [StringLength(60, MinimumLength = 3, ErrorMessage = "必须是[3,60]个字符")] public string Title { get; set; } //电影名称 [Display(Name = "上映时间")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)] public DateTime ReleaseDate { get; set; } //上映时间 [Display(Name = "电影类型")] [Required] public string Genre { get; set; } //电影类型 [Display(Name = "电影票价")] [Range(1, 100)] [DataType(DataType.Currency)] public decimal Price { get; set; } //电影票价 [Display(Name = "电影分级")] [StringLength(5)] [Required] public string Rating { get; set; } //电影分级 } }
MovieDBContext.cs
using System.Data.Entity; namespace ProjectThree.Models { public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } } }
Index.cshtml