ylbtech- ASP.NET MVC:MVC Movie App

  功能描述:MVC Movie App

2,TechnologyAndTheEnvironment(技术与环境)

操作系统:

windows

开发语言:

C#

开发框架:

ASP.NET MVC 3.0

数据库:

开发软件:

Microsoft Visual Studio 2010

 开发技术

ASP.NET MVC 4(Razor)

3,DatabaseDesign(数据库设计)
4,MVC
4,Models

/Models/Movie.cs

using System;
using System.Data.Entity; //命名空间包含提供对实体框架的核心功能的访问的类。
using System.ComponentModel.DataAnnotations; //命名空间提供定义 ASP.NET MVC 和 ASP.NET 数据控件的类的特性。 namespace MvcMovie.Models
{
public class Movie {
public int ID { get; set; } [Required(ErrorMessage = "Title is required")]
public string Title { get; set; } [Required(ErrorMessage = "Date is required")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ReleaseDate { get; set; } [Required(ErrorMessage = "Genre must be specified")]
public string Genre { get; set; } [Required(ErrorMessage = "Price Required")]
[Range(, , ErrorMessage = "Price must be between $1 and $100")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal Price { get; set; } [StringLength()]
public string Rating { get; set; }
} public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}

/Models/MovieUbtializer.cs

using System;
using System.Collections.Generic;
using System.Data.Entity; namespace MvcMovie.Models {
public class MovieInitializer : DropCreateDatabaseIfModelChanges<MovieDBContext> {
protected override void Seed(MovieDBContext context) {
var movies = new List<Movie> { new Movie { Title = "When Harry Met Sally",
ReleaseDate=DateTime.Parse("1989-1-11"),
Genre="Romantic Comedy",
Rating="G",
Price=7.99M}, new Movie { Title = "Ghostbusters ",
ReleaseDate=DateTime.Parse("1984-3-13"),
Genre="Comedy",
Rating="G",
Price=8.99M}, new Movie { Title = "Ghostbusters 2",
ReleaseDate=DateTime.Parse("1986-2-23"),
Genre="Comedy",
Rating="G",
Price=9.99M}, new Movie { Title = "Rio Bravo",
ReleaseDate=DateTime.Parse("1959-4-15"),
Genre="Western",
Rating="G",
Price=3.99M},
}; movies.ForEach(d => context.Movies.Add(d));
}
}
}
4,Views

/Views/Movices/Create.cshtml

@model MvcMovie.Models.Movie

@{
ViewBag.Title = "Create";
} <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend> <div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div> <div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
} <div>
@Html.ActionLink("Back to List", "Index")
</div>

/Views/Movices/Delete.cshtml

@model MvcMovie.Models.Movie

@{
ViewBag.Title = "Delete";
} <h2>Delete</h2> <h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Movie</legend> <div class="display-label">Title</div>
<div class="display-field">
@Html.DisplayFor(model => model.Title)
</div> <div class="display-label">ReleaseDate</div>
<div class="display-field">
@Html.DisplayFor(model => model.ReleaseDate)
</div> <div class="display-label">Genre</div>
<div class="display-field">
@Html.DisplayFor(model => model.Genre)
</div> <div class="display-label">Price</div>
<div class="display-field">
@Html.DisplayFor(model => model.Price)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}

/Views/Movices/Details.cshtml

@model MvcMovie.Models.Movie

@{
ViewBag.Title = "Details";
} <h2>Details</h2> <fieldset>
<legend>Movie</legend> <div class="display-label">Title</div>
<div class="display-field">
@Html.DisplayFor(model => model.Title)
</div> <div class="display-label">Release Date</div>
<div class="display-field">
@Html.DisplayFor(model => model.ReleaseDate)
</div> <div class="display-label">Genre</div>
<div class="display-field">
@Html.DisplayFor(model => model.Genre)
</div> <div class="display-label">Price</div>
<div class="display-field">
@Html.DisplayFor(model => model.Price)
</div>
<div class="display-label">Rating</div>
<div class="display-field">
@Html.DisplayFor(model => model.Rating)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>

/Views/Movices/Edit.cshtml

@model MvcMovie.Models.Movie

@{
ViewBag.Title = "Edit";
} <h2>Edit</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend> @Html.HiddenFor(model => model.ID) <div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div> <div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
} <div>
@Html.ActionLink("Back to List", "Index")
</div>

/Views/Movices/Index.cshtml

@model IEnumerable<MvcMovie.Models.Movie>

@{
ViewBag.Title = "Index";
} <h2>Index</h2> <p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Title
</th>
<th>
Release Date
</th>
<th>
Genre
</th>
<th>
Price
</th>
<th>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", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
} </table>

/Views/Movices/SearchIndex.cshtml

@model IEnumerable<MvcMovie.Models.Movie>

@{
ViewBag.Title = "SearchIndex";
} <h2>SearchIndex</h2> <p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get))
{
<p>Genre: @Html.DropDownList("movieGenre", "All")
@* &nbsp; Title: @Html.TextBox("SearchString") *@
<input type="submit" value="Filter" /></p>
}
</p>
<table>
<tr>
<th>
Title
</th>
<th>
Release Date
</th>
<th>
Genre
</th>
<th>
Price
</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.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
} </table>
4,Controllers

/Controllers/MoviesController.cs

//#define OverloadDelete
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Models; namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext(); // GET: /Movies/SearchIndex
#if ONE
public ActionResult SearchIndex(string Genre, string searchString)
{ var GenreLst = new List<string>();
GenreLst.Add("All"); var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.Genre = new SelectList(GenreLst); var movies = from m in db.Movies
select m; if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
} if (string.IsNullOrEmpty(Genre) || Genre == "All")
return View(movies);
else
{
return View(movies.Where(x => x.Genre == Genre));
} }
#else public ActionResult SearchIndex(string movieGenre, string searchString)
{ var GenreLst = new List<string>(); var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst); var movies = from m in db.Movies
select m; if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
} if (string.IsNullOrEmpty(movieGenre))
return View(movies);
else
{
return View(movies.Where(x => x.Genre == movieGenre));
} }
#endif //public ActionResult SearchIndex(string searchString)
//{
// var movies = from m in db.Movies
// select m; // if (!String.IsNullOrEmpty(searchString))
// {
// movies = movies.Where(s => s.Title.Contains(searchString));
// } // return View(movies);
//} [HttpPost]
public string SearchIndex(FormCollection fc, string searchString) {
return "<h3> From [HttpPost]SearchIndex: " + searchString + "</h3>";
} //
// GET: /Movies/ public ViewResult Index()
{
return View(db.Movies.ToList());
} //
// GET: /Movies/Details/5 public ActionResult Details(int id = )
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
} //
// GET: /Movies/Create public ActionResult Create()
{
return View();
} //
// POST: /Movies/Create [HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
} return View(movie);
} //
// GET: /Movies/Edit/5 public ActionResult Edit(int id = )
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
} //
// POST: /Movies/Edit/5 [HttpPost]
public ActionResult Edit(Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
} //
// GET: /Movies/Delete/5 public ActionResult Delete(int id = )
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
#if OverloadDelete
public ActionResult Delete(FormCollection fcNotUsed, int id = )
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
#else
//
// POST: /Movies/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id = ) {
Movie movie = db.Movies.Find(id);
if (movie == null) {
return HttpNotFound();
}
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
#endif }
}
6,Sample|Explain FreeDownload(示例|讲解案例下载)

百度网盘  http://pan.baidu.com/s/1i49zn73

请单击“Introduction to MVC 3”

作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

最新文章

  1. Redhat/Ubuntu/Windows下安装Docker
  2. 关于DIV+CSS和XHTML+CSS的理解
  3. MATLAB编译器
  4. 笔记本装双系统!win10+Linux!所有的坑自己一个个爬过来,纪念一下。
  5. Simple Factory Pattern
  6. 实现A-Z滑动检索菜单
  7. Android adb shell 常用命令
  8. 【bzoj 1901】Zju2112 Dynamic Rankings
  9. Go 新起点
  10. [USACO09JAN]Total Flow
  11. java后端导入excel将数据写入数据库
  12. 第15课-数据库开发及ado.net-数据库介绍,主键,外键,启动数据库,数据库身份验证方式,建表,分离数据库
  13. 历数PC发展史上的祖先们
  14. 〖Linux〗Ubuntu13.10搭建文件共享Samba服务器
  15. 《程序员面试金典》习题解答(C/C++)
  16. 用apscheduler处理调度任务,定时任务,重复任务
  17. AngularJS的简单订阅发布模式例子
  18. 【BZOJ4146】[AMPPZ2014]Divisors
  19. python之Memcached 安装及操作
  20. python对excel文件的读写操作

热门文章

  1. winds dlib人脸检测与识别库
  2. NPOI的使用Excel模板导出 可插入到指定行
  3. mongoDB的简单使用
  4. 详解SHOW PROCESSLIST显示哪些线程正在运行列出的状态
  5. 关于might_sleep的一点说明---CONFIG_DEBUG_ATOMIC_SLEEP【转】
  6. gpio子系统和pinctrl子系统(上)
  7. Launcher3自定义壁纸旋转后拉伸无法恢复
  8. golang之http
  9. vue—你必须知道的
  10. 跳转的两种实现方法setInterval和setTimeout