Ease & Easy ASP.NET MVC 开发框架(1) Hello World


 

本文介绍Ease&Easy开发框架如何简单搭建应用系统,通过Html.Grid的例子向大家逐步展示(如下图)。

image

 

1. 新建一个asp.net mvc项目,起名EaseEasy-Demo。

image

2. 通过NuGet获取Ease&Easy开发框架包。

image

3. 搜索“CoolCode”可看到有3个package。下载CoolCode.ServiceModel即可,它包含其他两个组件(CoolCode.Core和CoolCode.Web.Mvc)。

image

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.添加样式表及相应图片就大功告成了。

image

 

10.编译,运行 & Good Luck!

image

 

相关代码:

demo-1-HelloWorld.zip — 开发示例 Hello World

https://github.com/coolcode/free/tree/gh-pages/demo


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM