ASP.NET如何实现电影票信息的增删查改功能
创始人
2024-11-28 05:42:31
0次

题目

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)查询票价在某个区间的电影信息

效果

ASP.NET如何实现电影票信息的增删查改功能 
ASP.NET如何实现电影票信息的增删查改功能

(源码在文章结尾)

主要涉及知识点

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();
   GenreLst1.Add("是");
   GenreLst1.Add("否");
   ViewBag.movieOn = new SelectList(GenreLst1);
   //初始化电影类型下拉
   var GenreLst2 = new List();
   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("");
     }
     else
     {
      movies = movies.Where(s => s.Price >= low);
      movies = movies.Where(s => s.Price <= high);
     }
    }
    catch
    {
     Response.Write("");
     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 Movies { get; set; }
 }
}

Index.cshtml

@model IEnumerable
@{
 ViewBag.Title = "Index";
}

 @Html.ActionLink("新建", "Create")  @using (Html.BeginForm("Index", "Movie", FormMethod.Get))  {  

  电影是否上映:@Html.DropDownList("movieOn", "all")   电影类型:@Html.DropDownList("movieGenre", "all")   电影名称:@Html.TextBox("SearchString")   票价区间:@Html.TextBox("lowPrice")~@Html.TextBox("highPrice")     

 }

        @Html.DisplayNameFor(model => model.Title)          @Html.DisplayNameFor(model => model.ReleaseDate)          @Html.DisplayNameFor(model => model.Genre)          @Html.DisplayNameFor(model => model.Price)          @Html.DisplayNameFor(model => model.Rating)         @foreach (var item in Model) {         @Html.DisplayFor(modelItem => item.Title)          @Html.DisplayFor(modelItem => item.ReleaseDate)          @Html.DisplayFor(modelItem => item.Genre)          @Html.DisplayFor(modelItem => item.Price)          @Html.DisplayFor(modelItem => item.Rating)          @Html.ActionLink("编辑", "Edit", new { id=item.ID }) |    @Html.ActionLink("详情", "Details", new { id=item.ID }) |    @Html.ActionLink("删除", "Delete", new { id=item.ID }, new { onclick = "return confirm('确认删除吗?')" })      }

Create.cshtml

@model ProjectThree.Models.Movie
@{
 ViewBag.Title = "Create";
}
@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 
  
Movie
     @Html.ValidationSummary(true, "", new { @class = "text-danger" })       @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })    
  
      @Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })    
  
      @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })    
  
      @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })    
  
      @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })    
  
               
     }
 @Html.ActionLink("Back to List", "Index")

Edit.cshtml

@model ProjectThree.Models.Movie
@{
 ViewBag.Title = "Edit";
}

Edit

@using (Html.BeginForm()) {  @Html.AntiForgeryToken()     
Movie
     @Html.ValidationSummary(true, "", new { @class = "text-danger" })   @Html.HiddenFor(model => model.ID)       @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })              @Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })              @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })              @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })              @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })         @Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })     @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })                             }
 @Html.ActionLink("Back to List", "Index")

上一篇:在CentOS7中如何将ASP.NETCore应用程序部署至生产环境中

下一篇:Asp.Net Core如何发布和部署

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...