ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能
題目
1、使用Code First技術(shù)創(chuàng)建一個(gè)Movie數(shù)據(jù)模型。
public class Movie
{
public int ID { get; set; } //電影編號(hào)
public string Title { get; set; } //電影名稱(chēng)
public DateTime ReleaseDate { get; set; } //上映時(shí)間
public string Genre { get; set; } //電影類(lèi)型
public decimal Price { get; set; } //電影票價(jià)
public string Rating { get; set; } //電影分級(jí)
}
2、使用MVC相關(guān)技術(shù)實(shí)現(xiàn)數(shù)據(jù)的列表顯示和新增功能。
3、完成數(shù)據(jù)的編輯、刪除、明細(xì)和條件查詢(xún)等功能。
4、完成如下查詢(xún):
(1)查詢(xún)尚未上映電影的信息
(4)查詢(xún)票價(jià)在某個(gè)區(qū)間的電影信息
效果

(源碼在文章結(jié)尾)
主要涉及知識(shí)點(diǎn)
1、ASP.NET WEB MVC下的目錄結(jié)構(gòu)以及基礎(chǔ)編程
2、Linq查詢(xún)操作
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);
//初始化電影類(lèi)型下拉
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('左邊價(jià)格不可大于右邊!');</script>");
}
else
{
movies = movies.Where(s => s.Price >= low);
movies = movies.Where(s => s.Price <= high);
}
}
catch
{
Response.Write("<script>alert('必須輸入數(shù)字!');</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 = "電影編號(hào)")]
public int ID { get; set; } //電影編號(hào)
[Display(Name = "電影名稱(chēng)")]
[Required(ErrorMessage = "必填")]
[StringLength(60, MinimumLength = 3, ErrorMessage = "必須是[3,60]個(gè)字符")]
public string Title { get; set; } //電影名稱(chēng)
[Display(Name = "上映時(shí)間")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; } //上映時(shí)間
[Display(Name = "電影類(lèi)型")]
[Required]
public string Genre { get; set; } //電影類(lèi)型
[Display(Name = "電影票價(jià)")]
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; } //電影票價(jià)
[Display(Name = "電影分級(jí)")]
[StringLength(5)]
[Required]
public string Rating { get; set; } //電影分級(jí)
}
}
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")
電影類(lèi)型:@Html.DropDownList("movieGenre", "all")
電影名稱(chēng):@Html.TextBox("SearchString")
票價(jià)區(qū)間:@Html.TextBox("lowPrice")~@Html.TextBox("highPrice")
<input type="submit" value="查詢(xún)" />
</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('確認(rèn)刪除嗎?')" })
</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實(shí)現(xiàn)電影票信息的增刪查改功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤(pán)之文件夾實(shí)現(xiàn)
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤(pán)之文件夾實(shí)現(xiàn)...2006-09-09
給Repeater控件里添加序號(hào)的5種才常見(jiàn)方法介紹
Repeater是我們經(jīng)常用的一個(gè)顯示數(shù)據(jù)集的數(shù)據(jù)控件那么我們?cè)撛趺礊镽epeater控件添加序號(hào)呢?下面編輯為大家介紹幾種常用的為Repeater控件添加序號(hào)的方法2013-09-09
KindEditor圖片上傳的Asp.net代碼實(shí)例
KindEditor是一個(gè)不錯(cuò)的網(wǎng)頁(yè)在線(xiàn)編輯器,可是它只提供了asp,php,jsp上傳的類(lèi),沒(méi)有提供Asp.net上傳的類(lèi),廢話(huà)不多說(shuō),下面是ASP.NET的代碼2013-11-11
防止.NET應(yīng)用多實(shí)例運(yùn)行的有效方法
在開(kāi)發(fā)桌面應(yīng)用程序時(shí),可能會(huì)遇到這樣一種需求:限制程序只能同時(shí)運(yùn)行一個(gè)實(shí)例,如果用戶(hù)試圖再次啟動(dòng)同一個(gè)程序,應(yīng)該將已經(jīng)運(yùn)行的程序窗口置于前臺(tái)而不是啟動(dòng)一個(gè)新實(shí)例,這篇文章提供了一種通過(guò)檢測(cè)當(dāng)前程序?qū)嵗⒓せ钜堰\(yùn)行實(shí)例的方法,需要的朋友可以參考下2024-12-12
C#利用服務(wù)器實(shí)現(xiàn)客戶(hù)端之間通信
這篇文章主要為大家詳細(xì)介紹了C#利用服務(wù)器實(shí)現(xiàn)客戶(hù)端之間通信,感興趣的小伙伴們可以參考一下2016-08-08
Asp.net 2.0 無(wú)刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
簡(jiǎn)單三步實(shí)現(xiàn)圖片無(wú)刷新上傳:注意是上傳,至于上傳時(shí)的驗(yàn)證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會(huì)貼上來(lái)的。2013-06-06
asp.net使用AJAX實(shí)現(xiàn)無(wú)刷新分頁(yè)
AJAX(Asynchronous JavaScript and XML)是一種進(jìn)行頁(yè)面局部異步刷新的技術(shù)。用AJAX向服務(wù)器發(fā)送請(qǐng)求和獲得服務(wù)器返回的數(shù)據(jù)并且更新到界面中,不是整個(gè)頁(yè)面刷新,而是在頁(yè)面中使用Js創(chuàng)建XMLHTTPRequest對(duì)象來(lái)向服務(wù)器發(fā)出請(qǐng)求以及獲得返回的數(shù)據(jù)。2014-11-11
Asp.net實(shí)現(xiàn)無(wú)刷新調(diào)用后臺(tái)實(shí)體類(lèi)數(shù)據(jù)并以Json格式返回
本文主要分享了Asp.net實(shí)現(xiàn)無(wú)刷新調(diào)用后臺(tái)實(shí)體類(lèi)數(shù)據(jù)并以Json格式返回的具體實(shí)例方法,具有一定的參考價(jià)值,有需要的朋友可以看下2016-12-12

