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 ...
随机推荐
- 【Mysql优化】聚簇索引与非聚簇索引概念
必须为主键字段创建一个索引,这个索引就是所谓的"主索引".主索引与唯一索引的唯一区别是:前者在定义时使用的关键字是PRIMARY而不是UNIQUE. 首先明白两句话: innod ...
- NT式驱动和WDM式驱动
刚开始学习驱动,没什么基础,对于好多名词也不是很理解,感觉每天学的驱动都不一样.......今天看了书之后才知道,原来驱动分为NT式驱动和WDM式驱动两种.大概总结一下它们之间的区别. 对于NT式驱动 ...
- algorithm ch2 insertsort
刚开始看到insertsort,思路就是使用新来的元素与前述已经排好序的元素比较.然后进行插入或者跳到下一次比较. 实现的代码如下: void InsertSort(int *pArray, int ...
- Swift “ambiguous use of operator '>'”
http://stackoverflow.com/questions/25458548/swift-ambiguous-use-of-operator 3down votefavorite I h ...
- IC卡的传输协议(2)-块传输协议T=1续【转】
转自:http://bbs.ednchina.com/BLOG_ARTICLE_172025.HTM (3)容错操作 先来看一下容错的规则定义. * 复位应答后,第一个数据块是由终端发往IC卡的,而且 ...
- 使用pandas进行数据清洗
本文转载自:蓝鲸的网站分析笔记 原文链接:使用python进行数据清洗 目录: 数据表中的重复值 duplicated() drop_duplicated() 数据表中的空值/缺失值 isnull() ...
- Oracle rman 各种恢复
--恢复整个数据库run {shutdown immediate;startup mount;restore database;recover database;alter database open ...
- jquery发送json请求,给springmvc接收
js var obj = { 'name':name, 'desc':desc, 'scheduleStartTime':scheduleStartTime, 'scheduleEndTime':sc ...
- request.getServletContext()的问题!
ServletRequest的getServletContext方法是Servlet3.0添加的,这个可以看一下官方文档 http://docs.oracle.com/javaee/6/api/jav ...
- 【wordpress】 $wpdb 应用实例
<?php require_once('e:/php/wordpress/wp-blog-header.php');//注释掉这一句就出错了 global $wpdb; $a = $wpdb-& ...