现在的位置: 首页 > 综合 > 正文

新版本的MVC框架露下面

2011年08月14日 ⁄ 综合 ⁄ 共 2454字 ⁄ 字号 评论关闭

          之前也介绍过一下自己的MVC框架,想一下自己这个框架的构建估计要比asp.net mvc要早。当初构建她的时候也是因为用过MonoRail后的想法,从开始到现在估计差不多有一年了,在这个过程中不停的重构翻新再重构翻新…;经常N次的来回折磨后现在这个版本自己感觉也比较理想(其实对她挺满意的,自己先意淫一下);在构造这个框架的时候选用了webform作为基础进行改制,现在想起来这个选择是正确的,因为这样我就有更多的精力花在Controller,action,filter等功能设计上。

下面通过一个简单的leaveword功能来展示一下这个框架。

程序结构:

 

程序主要包括两个页面和一个Controller的类。

Leveword.aspx是留言列表和添加页面。

AddlLW.asp留言提交数据处理。

对于Controller定义如下:

[NClay.Web.Controller("~/")]

    public class RootController:NClay.Web.ControllerBase

    {

        public void leaveword()

        {

         

        }

        public void AddLW(LeaveWord leaveword)

        {

          

        }

 }

对于pageaction的对应关系,细心的朋友应该发现action的名称和page一样.

 

Leveword.aspx页面的HTML

<!内容列表-->

<%

    HtmlHelper.Each<MVCSample.LeaveWord>("items", e => {%>

<h1 style="font-size:large"><%=e.Data.Title %> <a href="mailto:<%=e.Data.EMail %>">(<%=e.Data.NickName %>)</a><%=e.Data.CreateDate %></h1>

<p><%=e.Data.Content %></p>

<hr />

<% });%>

<!留言添加处理-->

<%

    NClay.Web.Helpers.JQueryValidator jv;

     %>

<form id="AddLeaveword" action="AddLW.aspx" method="post">

<%jv = new NClay.Web.Helpers.JQueryValidator { NonNull =true,Tip="内容标题不能为空!" }; %>

<span class="formText">标题</span><input name="Title" <%=jv %> /><BR />

<%jv = new NClay.Web.Helpers.JQueryValidator { NonNull =true,Tip="昵称不能为空!" }; %>

<span class="formText">昵称</span><input name="NickName" <%=jv %> /><BR />

<%jv = new NClay.Web.Helpers.JQueryValidator { NonNull =true,Tip="邮件地址不能为空!" }; %>

<span class="formText">邮件地址</span><input name="EMail" <%=jv %> /><BR />

<%jv = new NClay.Web.Helpers.JQueryValidator { NonNull =true,Tip="内容不能为空!" }; %>

<span class="formText">内容</span><textarea name="Content" <%=jv %> style="width: 340px; height: 69px;"></textarea><BR />

<input type="button" value="确定" onclick="editdata('AddLeaveword','leaveword.aspx')" />

</form>

Leavework.aspxaction处理很简单,把留言记录读取出来

        public void leaveword()

        {

             Expression exp = new Expression();

              IList<LeaveWord> items = exp.List<LeaveWord>(ModelContext.LeaveWord.CreateDate.DESC);

              Context["items"] = items;

          

        }

对框架而言asxp.cs已经不存在,也没存在的必要。

留言保存处理

       Leavework.aspx中留言信息通过ajax的方式提交给AddLW.aspx

<input type="button" value="确定" onclick="editdata('AddLeaveword','leaveword.aspx')" />

这功能是根据自己的需要在JQueryajaxForm封装了的方法。

由于AddLW.aspx只用于数据的接收,所以并不需要什么页面描述,只需要相关action添加处理代码就可以了。

        public void AddLW(LeaveWord leaveword)

        {

            Context.Catch(e => {

                leaveword.CreateDate = DateTime.Now;

                DaoContext.Add(leaveword);

            });

            Context.OutException();

        }

到这里需要编写的代码都已经完,接下来看一下效果吧:)

 

 

下载代码

抱歉!评论已关闭.