MVC3笔记
1 public ActionResult Index(string movieGenre, string searchString) 2 { 3 var GenreLst = new List<string>(); 4 5 var GenreQry = from d in db.Movies 6 orderby d.Genre 7 select d.Genre; 8 9 GenreLst.AddRange(GenreQry.Distinct()); 10 ViewBag.movieGenre = new SelectList(GenreLst); 11 12 var movies = from m in db.Movies 13 select m; 14 15 if (!String.IsNullOrEmpty(searchString)) 16 { 17 movies = movies.Where(s => s.Title.Contains(searchString)); 18 } 19 20 if (!String.IsNullOrEmpty(movieGenre)) 21 { 22 movies = movies.Where(x => x.Genre == movieGenre); 23 } 24 25 return View(movies); 26 } 27 28 /// 29 30 // 知识点一:web.config数据库连接 31 32 <add name="MovieDBContext" connectionString="Data Source=.;Initial Catalog = Movies;Integrated Security=True" providerName="System.Data.SqlClient" /> 33 34 //知识点二:初始化数据库及数据 35 36 using System.ComponentModel.DataAnnoations; 37 //在Model类的Movies.cs里添加引用和下面的初始化类 38 39 public class movieInitializer:DropCreateDatabaseIfModelChanges<MovieDBContext> 40 { 41 protected override void Seed(MovieDBContext context) 42 { 43 var movies = new List<Movie> 44 { 45 new Movie{Title = "when harray met sally", Price=4.6M}, new Movie{Title = "Ghost", Price = 200.0M}, 46 }; 47 48 movies.ForEach(d => context.Movies.Add(d)); 49 } 50 } 51 52 //然后在Global.asax的protected void Application_Start()里添加如下代码: 53 Database.SetInitializer<MovieDBContext>(new MovieInitializer()); 54 55 //知识点三:添加MoviesControllers 56 // Details 注意 ViewResult 要改成:ActionResult 因为需要添加个HttpNotFound而它是不能返回 ViewResult 对象的 57 58 public ActionResult Details(int id = 0) 59 { 60 Movie movie = db.Movies.Find(id); 61 if (movie == null) 62 { 63 return HttpNotFound(); 64 } 65 66 return View(movie); 67 } 68 //像Edit 和 Delete 最好给一个初始值,以防提交的 ID 是空的 69 70 public ActionResult Edit(int id = 2) 71 { 72 Movie movie = db.Movies.Find(id); 73 if (movie == null) 74 { 75 return HttpNotFound(); 76 } 77 78 return View(movie); 79 } 80 81 //MVC 3 改进了删除功能,加上了删除确认,目的是防止恶意代码没有经过确认就删除数据 82 // 当点击删除连接时只是返回确认信息。 83 public ActionResult Delete(int id) 84 { 85 Movie movie = db.Movies.Find(id); 86 87 return View(movie); 88 } 89 90 //!! a) 请注意查看View 里的Delete.cshtml 文件删除源文件: 91 @using (Html.BeginForm()){ 92 <P> 93 <input type="submit" value="Delete" /> | 94 @Html.ActionLink("Back to List", "Index"); 95 </p> 96 97 ... 98 } 99 100 //!! b) 在生成html的确认删除源文件里可以看到自动生成了action="/movies.Delete/1": 101 <form action="/movies/Delete/1" method="post"> 102 <p> 103 <input type="submit" value="Delete" /> | 104 <a href="/movies"> Back to List </a> 105 </p> 106 </form> 107 108 // c) 下面是确认删除后执行的动作 109 [HttpPost, ActionName("Delete")] 110 public ActionResult DeleteConfirmed(int id=0) 111 { 112 Movie movie = db.Movies.Find(id); 113 if (movie == null) 114 { 115 return HttpNotFound(); 116 } 117 118 db.Movies.Remove(movie); 119 db.SaveChanges(); 120 121 return RedirecToAction("Index"); 122 } 123 124 // 知识点四:内容搜索过滤功能 125 // 先来看SearchIndex.cshtml 视图页 126 127 @using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get)) 128 { 129 <p> 130 Genre:@Html.DropDownList("movieGenre", "All") 131 </P> 132 <p> 133 Title:@Html.TextBox("SearchString") 134 <input type="submit" value="Filter" /> 135 </p> 136 } 137 138 //---- 分支1. 指定表单提交方式和路径等 139 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new {name = "nbform", id = "nbform"})) 140 141 //---- 分支2. 指定表单提交的数据方式 142 @using (Html.BeginForm("ImportExcel", "Stock", FormMethod.Post, new {enctype="multipart/form-data"})) 143 144 // 下面是Controller: 145 146 public AcitonResult SearchIndex(string movieGenre, string searchString) 147 { 148 // 内容在顶部 149 } 150 151 // 下面是生成的html页面源代码: 152 153 <form action="/Movies/SearchIndex" method="get"> 154 Genre: 155 <select id="movieGenre" name="movieGenre"> 156 <option value="">All</option> 157 <option>comedy</option> 158 <option>Romanti Comedy</option> 159 </select> 160 <p> 161 Title: 162 <input id="SearchString" name="Search" type="text" value=""/> 163 <input type="submit" value="Filter" /> 164 </p> 165 </from> 166 167 //知识点五:Movie类和数据库连接上下文类MovieDBContext,注意引用using System.Data.Entity 168 using System.Data.Entity; 169 public class Movie 170 { 171 public int ID {get; set;} 172 public string Title {get; set;} 173 public decimal Price {get; set;} 174 } 175 176 public class MovieDBContext:DbContext 177 { 178 public DbSet<Movie> Movies{get; set;} 179 } 180 181 //知识点六:字段规则确认验证 182 public int ID {get; set;} 183 [Required(ErrorMessage="标题必需要填写")] 184 public string Title{get; set;} 185 [Required(ErrorMessage="Price Required")] 186 [Range(1, 100, ErrorMessage="Price must be between $1 and $100")] 187 [DisplayFormat(DataFormatString="{0:c}")] 188 public decimal Price{get; set;}
转帖自小氕
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。