HTML5技术

Nancy之基于Nancy.Hosting.Aspnet的小Demo - Catcher8(4)

字号+ 作者:H5之家 来源:博客园 2016-01-21 17:05 我要评论( )

] = _ = ShowMovieEditPage(_.movieId); ] = _ = 3 { 4 var movie = this .BindMovie (); 5 return MovieEdit(movie); 6 }; 然后添加ShowMovieEditPage和MovieEdit这两个方法 ShowMovieEditPage( int movieId) 2 {

] = _ => 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配置

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍 - 在路在的张

    随应潮流-基于ABP+Angularjs现代化应用软件开发框架(1)-总体介绍 -

    2017-04-22 08:04

  • 基于CSS的个人网页 - 汕大-吴广林

    基于CSS的个人网页 - 汕大-吴广林

    2017-04-21 12:00

  • PixiJS - 基于 WebGL 的超快 HTML5 2D 渲染引擎 - 梦想天空(山边小溪)

    PixiJS - 基于 WebGL 的超快 HTML5 2D 渲染引擎 - 梦想天空(山边小

    2017-04-12 08:02

  • 任务调度之持久化(基于Quartz.net) - 蓝建荣

    任务调度之持久化(基于Quartz.net) - 蓝建荣

    2017-03-28 12:03

网友点评