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

所属分类: 网络编程 / ASP.NET 阅读数: 478
收藏 0 赞 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)查询票价在某个区间的电影信息

效果

这里写图片描述 
这里写图片描述

(源码在文章结尾)

主要涉及知识点

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

@model IEnumerable<ProjectThree.Models.Movie>
@{
 ViewBag.Title = "Index";
}
<p>
 @Html.ActionLink("新建", "Create")
 @using (Html.BeginForm("Index", "Movie", FormMethod.Get))
 {
 <p>
  电影是否上映:@Html.DropDownList("movieOn", "all")
  电影类型:@Html.DropDownList("movieGenre", "all")
  电影名称:@Html.TextBox("SearchString")
  票价区间:@Html.TextBox("lowPrice")~@Html.TextBox("highPrice")
  <input type="submit" value="查询" />
 </p>
 }
</p>
<table class="table">
 <tr>
  <th>
   @Html.DisplayNameFor(model => model.Title)
  </th>
  <th>
   @Html.DisplayNameFor(model => model.ReleaseDate)
  </th>
  <th>
   @Html.DisplayNameFor(model => model.Genre)
  </th>
  <th>
   @Html.DisplayNameFor(model => model.Price)
  </th>
  <th>
   @Html.DisplayNameFor(model => model.Rating)
  </th>
  <th></th>
 </tr>
@foreach (var item in Model) {
 <tr>
  <td>
   @Html.DisplayFor(modelItem => item.Title)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.ReleaseDate)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.Genre)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.Price)
  </td>
  <td>
   @Html.DisplayFor(modelItem => item.Rating)
  </td>
  <td>
   @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('确认删除吗?')" })
  </td>
 </tr>
}
</table>

Create.cshtml

@model ProjectThree.Models.Movie
@{
 ViewBag.Title = "Create";
}
@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 <div class="form-horizontal">
  <h4>Movie</h4>
  <hr />
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  <div class="form-group">
   @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Create" class="btn btn-default" />
   </div>
  </div>
 </div>
}
<div>
 @Html.ActionLink("Back to List", "Index")
</div>

Edit.cshtml

@model ProjectThree.Models.Movie
@{
 ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()
 <div class="form-horizontal">
  <h4>Movie</h4>
  <hr />
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  @Html.HiddenFor(model => model.ID)
  <div class="form-group">
   @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Save" class="btn btn-default" />
   </div>
  </div>
 </div>
}
<div>
 @Html.ActionLink("Back to List", "Index")
</div>

源码地址

http://download.csdn.net/detail/double2hao/9710754

以上所述是小编给大家介绍的ASP.NET实现电影票信息的增删查改功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

运行page页面时的事件执行顺序及页面的回发与否深度了解

page页面时的事件执行顺序的了解对于一些.net开发者起到者尤关重要的作用;页面的回发与否会涉及到某些事件执行与不执行,在本文中会详细介绍,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Web.config(应用程序的配置信息)总结

Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中,接下来详细介绍一下配置情况,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C#时间格式化(Datetime)用法详解

C#时间格式化Datetime.ToString参数format格式详细用法,本文将进行介绍,感兴趣的朋友可以了解下啊
收藏 0 赞 0 分享

实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上

一直想实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上。此功能就是当鼠标经过时RadioButtonList或CheckBoxList每一个Item时,让Item有特效显示,离开时,恢复原样有演示动画,感兴趣的朋
收藏 0 赞 0 分享

Asp.net 图片文件防盗链(尊重劳动成果)及BeginRequest事件学习

关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了;反盗链的程序其实很简单,熟悉ASP.NET 应用程序生命周期的话很容易就可以写一个,运用HttpModule在BeginRequest事件中拦截请求就ok了
收藏 0 赞 0 分享

asp.net Grid 导出Excel实现程序代码

看了FineUI中的将Grid导出为Excel的实现方法,实际上是可以非常简单。看来很难的问题,变换一种思路就可以非常简单
收藏 0 赞 0 分享

使用C#处理WebBrowser控件在不同域名中的跨域问题

我们在做web测试时,经常会使用WebBrowser来进行一些自动化的任务而有些网页上面会用IFrame去嵌套别的页面,这些页面可能不是在相同域名下的,这时就会出现跨域问题,无法直接在WebBrowser中获取到IFrame中的元素,接下来介绍如何解决此问题,需要了解的朋友可以参
收藏 0 赞 0 分享

.NET 下运用策略模式(组合行为和实体的一种模式)

我简单的理解策略模式就是把行为(方法)单独的抽象出来,并采用组合(Has-a)的方式,来组合行为和实体的一种模式比如,.NET中对数组排序的Sort的方法就是一个策略模式的实现模板
收藏 0 赞 0 分享

使用Entity Framework(4.3.1版本)遇到的问题整理

在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题:更新没有设置主键的表、更改Code-First的默认连接、检测字符串截断错误,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net利用JQuery AJAX实现无刷新评论思路与代码

Asp.net利用JQuery AJAX实现无刷新评论,此功能是每一个从事asp.net开发者的朋友都希望实现的,本文利用闲暇时间整理了一些,有需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多