本文介绍Ease&Easy开发框架如何简单搭建应用系统,通过Html.Grid的例子向大家逐步展示(如下图)。
1. 新建一个asp.net mvc项目,起名EaseEasy-Demo。
2. 通过NuGet获取Ease&Easy开发框架包。
3. 搜索“CoolCode”可看到有3个package。下载CoolCode.ServiceModel即可,它包含其他两个组件(CoolCode.Core和CoolCode.Web.Mvc)。
4. 新建EF实体和DbContext,用于测试框架的Grid控件。
public class DemoContext : DbContext { public DbSet<Blog> Blogs { get; set; } public static void SetInitializer() { Database.SetInitializer(new DemoContextInitializer()); } class DemoContextInitializer : DropCreateDatabaseIfModelChanges<DemoContext> { protected override void Seed(DemoContext context) { for (int i = 0; i < 102; i++) { context.Blogs.Add(new Blog { Title = "MVC 框架"+i, Author = "Bruce Lee" }); } } } }
public class Blog { public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } }
5.Application_Start方法调用 DemoContext.SetInitializer();
6.Web.config 配置数据库连接字符串DemoContext。
<connectionStrings> <add name="DemoContext" connectionString="Data Source=.; Initial Catalog=EaseEasyDemo;User ID=sa;Password=xx;Persist Security Info=true" providerName="System.Data.SqlClient"/> </connectionStrings>
7.添加Controller,命名为DemoController。添加Action,返回IQueryable<> 类型数据源。
public ActionResult Index() { var db = new DemoContext(); return View(db.Blogs); }
8.添加View,命名为Index.cshtml。Html.Grid会根据Model生成表格,默认支持分页。
<link href="@Url.Content("~/Content/EaseEasy.css")" rel="stylesheet" type="text/css" /> <h2>EaseEasy: Hello World!</h2> @( Html.Grid<Blog>().Columns(c => { c.Column(x=>x.Id).Width(102).Sortable(true); c.Column(x=>x.Title).Width(102).Header("标题").Sortable(true); c.Column(x=>x.Author).Width(102).Header("作者").Sortable(true); }) .IsAjax(false) )
9.添加样式表及相应图片就大功告成了。
10.编译,运行 & Good Luck!
相关代码:
demo-1-HelloWorld.zip — 开发示例 Hello World