ASP. NET allows you to add, query, modify, and delete movie tickets.

Source: Internet
Author: User
Tags actionlink

ASP. NET allows you to add, query, modify, and delete movie tickets.

Question

1. Use Code First technology to create a Movie data model.

Public class Movie {public int ID {get; set;} // Movie number public string Title {get; set;} // Movie name public DateTime ReleaseDate {get; set ;} // release time public string Genre {get; set;} // movie type public decimal Price {get; set;} // movie fare public string Rating {get; set ;} // classification of movies}

2. Use MVC-related technologies to display data lists and add new functions.

3. Complete data editing, deletion, details, and conditional query.

4. Complete the following query:

(1) query information about movies not released

(4) query the movie information of a fare in a certain range

Effect

 

(The source code is at the end of the article)

Key knowledge points

1. directory structure and basic programming in ASP. NET WEB MVC

2. query by using Linq

3. Code First

4. creation and use of View templates

Main Code

MovieController. cs

Using ProjectThree. models; 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 highPr Ice) {// initialize the drop-down var GenreLst1 = new List <string> (); GenreLst1.Add ("yes"); GenreLst1.Add ("no"); ViewBag. movieOn = new SelectList (GenreLst1); // initialize the drop-down var GenreLst2 = new List <string> (); var GenreQry = from d in db. movies orderby d. genre select d. genre; GenreLst2.AddRange (GenreQry. distinct (); // deduplicate 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 ("yes") {movies = movies. where (s => DateTime. compare (dtNow, s. releaseDate)> 0);} else if (movieOn. equals ("no") {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 ('left price cannot be greater than right! '); </Script> ");} else {movies = movies. where (s => s. price> = low); movies = movies. where (s => s. price <= high) ;}} catch {Response. write ("<script> alert ('a number is required! '); </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 = "Movie number")] public int ID {get; set ;} // movie no. [Display (Name = "movie Name")] [Required (ErrorMessage = "Required")] [StringLength (60, MinimumLength = 3, errorMessage = "must be [3, 60] characters")] public string Title {get; set;} // movie Name [Display (Name = "")] [DataType (DataType. date)] [DisplayFormat (DataFormatString = "{0: yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ReleaseDate {get; set ;} // release date [Display (Name = "movie type")] [Required] public string Genre {get; set ;} // movie type [Display (Name = "movie fare")] [Range (1,100)] [DataType (DataType. currency)] public decimal Price {get; set;} // movie fare [Display (Name = "movie Classification")] [StringLength (5)] [Required] public string Rating {get; set ;}// movie classification }}

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 ("new", "Create") @ using (Html. beginForm ("Index", "Movie", FormMethod. get) {<p> whether the film is released: @ Html. dropDownList ("movieOn", "all") movie type: @ Html. dropDownList ("movieGenre", "all") Movie name: @ Html. textBox ("SearchString") fare range: @ Html. textBox ("lowPrice ")~ @ Html. textBox ("highPrice ") <input type = "submit" value = "query"/> </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> </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", "Edit", new {id = item. ID}) | @ Html. actionLink ("Details", "Details", new {id = I Tem. ID}) | @ Html. actionLink ("Delete", "Delete", new {id = item. ID}, new {onclick = "return confirm ('Are you sure you want to delete it? ') "}) </Td> </tr >}</table>

Create. cshtml

@model ProjectThree.Models.Movie@{ ViewBag.Title = "Create";}@using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal">  

Edit. cshtml

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

Source Code address

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

The above is a small series of ASP. NET to add, query, modify, and delete movie ticket information. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.