] = _ => ShowMovieEditPage(_.movieId); ] = _ => 3 { 4 var movie = this.Bind<Movie>(); 5 return MovieEdit(movie); 6 };
然后添加ShowMovieEditPage和MovieEdit这两个方法
ShowMovieEditPage(int movieId) 2 { ; ; 5 DynamicParameters dynamicParameters = new DynamicParameters(); , movieId); (IDbConnection conn = OpenConnection()) 9 { 10 var movieViewModel = conn.Query<MovieViewModel>(getOneMovieStoredProcedure, dynamicParameters, null, true, null, CommandType.StoredProcedure).SingleOrDefault(); 11 ViewBag.typeList = conn.Query<MovieType>(getALLTypeStoredProcedure, null, null, true, null, CommandType.StoredProcedure); , movieViewModel]; 13 14 } 15 }
ShowMovieEditPage
MovieEdit(Movie movie) 2 { ; 4 DynamicParameters dynamicParameters = new DynamicParameters(); , movie.MovieId); , movie.MovieName); , movie.MovieTypeId); (IDbConnection conn = OpenConnection()) 10 { 11 conn.Execute(updateMovieStoredProcedure, dynamicParameters, null, null, CommandType.StoredProcedure); ); 13 } 14 }
MovieEdit添加一个Movie的Edit视图
1 @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<MovieDemo.ViewModels.MovieViewModel> 2 @{ ; ; 5 } method=> > >电影名称</label> > =id=name=value=/> 12 </div> 13 </div> > >电影类型</label> > name==> 18 @foreach (var item in (System.Collections.Generic.List<MovieDemo.Models.MovieType>)ViewBag.typeList) 19 { 20 if (Model.MovieTypeId == item.TypeId) 21 { value=>@item.TypeName</option> 23 } { >@item.TypeName</option> 27 } 28 } 29 30 </select> 31 </div> 32 </div> > > value==/> 36 </div> 37 </div> 38 </form>
Edit.cshtml然后即可跑起来了。
其中用到了一个模型绑定,需要添加Nancy.ModelBinding引用,有了这个绑定,省了很多事!!
var movie = this.Bind<Movie>();
同理,增加和删除也是同样的做法,下面是MovieModule.cs的完整代码
1 using MovieDemo.Models; 2 using MovieDemo.ViewModels; 3 using Dapper; 4 using Nancy; 5 using Nancy.ModelBinding; 6 using System.Data; 7 using System.Data.SqlClient; 8 using System.Linq; MovieDemo.Modules 11 { MovieModule : NancyModule 13 { ) 15 { ] = _ => ShowMovieIndexPage(); ] = _ => ShowMovieEditPage(_.movieId); ] = _ => 20 { 21 var movie = this.Bind<Movie>(); 22 return MovieEdit(movie); 23 }; ] = _ => ShowMovieCreatePage(); ] = _ => 27 { 28 var movie = this.Bind<Movie>(); 29 return MovieCreate(movie); 30 }; ] = _ => MovieDelete(_.movieId); 33 } _sqlconnection = ; SqlConnection OpenConnection() 39 { 40 SqlConnection connection = new SqlConnection(_sqlconnection); 41 connection.Open(); 42 return connection; 43 } MovieDelete(int movieId) 46 { ; 48 DynamicParameters dynamicParameters = new DynamicParameters(); , movieId); 50 using (IDbConnection conn = OpenConnection()) 51 { 52 conn.Execute(deleteMovieStoredProcedure, dynamicParameters, null, null, CommandType.StoredProcedure); ); 54 } 55 } MovieCreate(Movie movie) 58 { ; 60 DynamicParameters dynamicParameters = new DynamicParameters(); , movie.MovieName); , movie.MovieTypeId); , movie.MovieAddTime); (IDbConnection conn = OpenConnection()) 66 { 67 conn.Execute(createMovieStoredProcedure, dynamicParameters, null, null, CommandType.StoredProcedure); ); 69 } 70 } ShowMovieCreatePage() 73 { ; 75 using (IDbConnection conn = OpenConnection()) 76 { 77 MovieTypeListViewModel viewModel = new MovieTypeListViewModel 78 { 79 MovieTypes = conn.Query<MovieType>(getALLTypeStoredProcedure, null, null, true, null, CommandType.StoredProcedure) 80 }; , viewModel]; 82 } 83 } MovieEdit(Movie movie) 86 { ; 88 DynamicParameters dynamicParameters = new DynamicParameters(); , movie.MovieId); , movie.MovieName); , movie.MovieTypeId); (IDbConnection conn = OpenConnection()) 94 { 95 conn.Execute(updateMovieStoredProcedure, dynamicParameters, null, null, CommandType.StoredProcedure); ); 97 } 98 } ShowMovieEditPage(int movieId) 101 { ; ; 104 DynamicParameters dynamicParameters = new DynamicParameters(); , movieId); (IDbConnection conn = OpenConnection()) 108 { 109 var movieViewModel = conn.Query<MovieViewModel>(getOneMovieStoredProcedure, dynamicParameters, null, true, null, CommandType.StoredProcedure).SingleOrDefault(); 110 ViewBag.typeList = conn.Query<MovieType>(getALLTypeStoredProcedure, null, null, true, null, CommandType.StoredProcedure); , movieViewModel]; 112 113 } 114 } ShowMovieIndexPage() 117 { 118 using (IDbConnection conn = OpenConnection()) 119 { ; 121 MovieListViewModel viewModel = new MovieListViewModel 122 { 123 Movies = conn.Query<MovieViewModel>(getAllMoviesStoredProcedure, 124 null, null, true, null, CommandType.StoredProcedure) 125 }; , viewModel]; 127 } 128 } 129 } 130 }
MovieModule.cs在添加相应的视图即可完成。
十一、下面还需要简单讲一下Bootstrapper
这个东西可以简单认为是引导程序,你可以自定义一些鬼东西,把它当作一个容器来用,
还可以通过它来实现依赖注入,里面有个默认的 TinyIoC 。。。。。详细介绍请看https://github.com/NancyFx/Nancy/wiki/Bootstrapper
下面是我的CustomBootstrapper.cs配置