ASP.NET实现电影票信息的增删查改功能
程序员文章站
2022-05-26 09:53:32
题目
1、使用code first技术创建一个movie数据模型。
public class movie
{
public int id { get;...
题目
1、使用code first技术创建一个movie数据模型。
public class movie { public int id { get; set; } //电影编号 public string title { get; set; } //电影名称 public datetime releasedate { get; set; } //上映时间 public string genre { get; set; } //电影类型 public decimal price { get; set; } //电影票价 public string rating { get; set; } //电影分级 }
2、使用mvc相关技术实现数据的列表显示和新增功能。
3、完成数据的编辑、删除、明细和条件查询等功能。
4、完成如下查询:
(1)查询尚未上映电影的信息
(4)查询票价在某个区间的电影信息
效果
(源码在文章结尾)
主要涉及知识点
1、asp.net web mvc下的目录结构以及基础编程
2、linq查询操作
3、code first
4、各模板view的建立和使用
主要代码
moviecontroller.cs
using projectthree.models; using system; 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 highprice) { //初始化电影是否上映下拉 var genrelst1 = new list<string>(); genrelst1.add("是"); genrelst1.add("否"); viewbag.movieon = new selectlist(genrelst1); //初始化电影类型下拉 var genrelst2 = new list<string>(); var genreqry = from d in db.movies orderby d.genre select d.genre; genrelst2.addrange(genreqry.distinct()); //去重 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("是")) { movies = movies.where(s => datetime.compare(dtnow, s.releasedate) > 0); } else if (movieon.equals("否")) { 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('左边价格不可大于右边!');</script>"); } else { movies = movies.where(s => s.price >= low); movies = movies.where(s => s.price <= high); } } catch { response.write("<script>alert('必须输入数字!');</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 = "电影编号")] public int id { get; set; } //电影编号 [display(name = "电影名称")] [required(errormessage = "必填")] [stringlength(60, minimumlength = 3, errormessage = "必须是[3,60]个字符")] public string title { get; set; } //电影名称 [display(name = "上映时间")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}",applyformatineditmode = true)] public datetime releasedate { get; set; } //上映时间 [display(name = "电影类型")] [required] public string genre { get; set; } //电影类型 [display(name = "电影票价")] [range(1, 100)] [datatype(datatype.currency)] public decimal price { get; set; } //电影票价 [display(name = "电影分级")] [stringlength(5)] [required] public string rating { get; set; } //电影分级 } }
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("新建", "create") @using (html.beginform("index", "movie", formmethod.get)) { <p> 电影是否上映:@html.dropdownlist("movieon", "all") 电影类型:@html.dropdownlist("moviegenre", "all") 电影名称:@html.textbox("searchstring") 票价区间:@html.textbox("lowprice")~@html.textbox("highprice") <input type="submit" value="查询" /> </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> <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", new { id=item.id }) | @html.actionlink("详情", "details", new { id=item.id }) | @html.actionlink("删除", "delete", new { id=item.id }, new { onclick = "return confirm('确认删除吗?')" }) </td> </tr> } </table>
create.cshtml
@model projectthree.models.movie @{ viewbag.title = "create"; } @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>movie</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.title, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.title, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.releasedate, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.releasedate, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.releasedate, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.genre, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.genre, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.genre, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.price, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.price, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.rating, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.rating, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.rating, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </div> } <div> @html.actionlink("back to list", "index") </div>
edit.cshtml
@model projectthree.models.movie @{ viewbag.title = "edit"; } <h2>edit</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>movie</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) @html.hiddenfor(model => model.id) <div class="form-group"> @html.labelfor(model => model.title, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.title, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.releasedate, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.releasedate, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.releasedate, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.genre, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.genre, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.genre, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.price, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.price, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.rating, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.rating, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.rating, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="save" class="btn btn-default" /> </div> </div> </div> } <div> @html.actionlink("back to list", "index") </div>
源码地址
以上所述是小编给大家介绍的asp.net实现电影票信息的增删查改功能,希望对大家有所帮助