MVC Movie App
| 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")
@* 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/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
MVC Movie App的更多相关文章
- [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
本文转自:https://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-o ...
- MVC 5 App 通过 Facebook OAuth2 登陆(Sign-on)的问题
今天做了下MVC 5 App通过Google, Twitter, Linkedin 和 Facebook进行登录的例子, 算是对Asp.net Identity的一个入门,做的过程中发现了如下的问题, ...
- 004.Create a web app with ASP.NET Core MVC using Visual Studio on Windows --【在 windows上用VS创建mvc web app】
Create a web app with ASP.NET Core MVC using Visual Studio on Windows 在 windows上用VS创建mvc web app 201 ...
- Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2- ...
- Asp.Net Web Forms/MVC/Console App中使用Autofac
本来简单介绍了Autofac在Asp.Net Web Forms中的应用,后来又添加了mvc.控制台应用程序中使用Autofac,详情请看源码. ASP.NET Web Forms使用Autofac, ...
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(8)-MVC与EasyUI DataGrid 分页
系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI Datagrid在加载的时候会提交一些分页的信息到后台,我们需要根据这些信息来进行数据分页再次返回到前台 实 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查
系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...
- MVC解决Json DataGrid返回的日期格式是/Date(20130450000365)
实际上是Json格式化问题,我们应该在返回json的时候进行格式化,我们需要重写系统的JsonResult类 using System; using System.Collections.Generi ...
随机推荐
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- Linux的yum命令——(八)
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...
- POJ3468(线段树区间增加,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 81519 ...
- Crontab无法自动执行,直接运行脚本却能执行
Crontab无法自动执行,直接运行脚本却能执行 http://bbs.chinaunix.net/thread-1926428-1-1.html crontab -e crontab内容为* * * ...
- c++文件流写入到execl中
#include <iostream> #include <fstream> #include <string> using namespace std; int ...
- 字节、字、bit、byte的关系[转]
字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit) 一个字的字长为16 一个字节的字长是8 bps ...
- cobbler自动重装
如果物理机上想更换操作系统 yum -y install http://mirrors.163.com/centos/7/extras/x86_64/Packages/epel-release-7-9 ...
- win10下MongoDB安装
下载 MongoDB 官网下载链接 如果被墙的话,请点击 mongodb-win32-x86_64-2008plus-ssl-3.4.1-signed.msi 下载 选择custom安装方式,手动切换 ...
- MYSQL从入门到放弃系列:mysql基础语法
Mysql基本语法 启动MySQL net start mysql 连接与断开服务器 mysql -h 地址 -P 端口 -u 用户名 -p 密码 跳过权限验证登录MySQL mysqld --ski ...
- [BZOJ 2752] 高速公路
Link: BZOJ 2752 传送门 Solution: 虽然有期望,但实际上就是除了个总数…… 此题计算总代价明显还是要使用对每个$w_i$计算贡献的方式: $w_i的贡献为w_i*(i-l+1) ...
