代码改变世界

Asp.net mvc +Ajax +Extjs+NHibernate 系列之MVC

2010-07-26 00:39  c#在路上  阅读(1149)  评论(0)    收藏  举报

概述

Asp.net mvc 是微软的开源项目,http://www.asp.net/mvc此处有开源地址,大家可以去下载源代码。不用去想,MVC即,modelviewcontroller。而微软提供的框架,使我们不用关心框架本身,而只需要去实现自己的部分,去构建属于自己的网站即可。

详解

Model层,所有项目中所有实体在该层表示,例如数据库中的对象等。前面提到的SystemConfigMVCDemo.Entity层中的实体便可以使用。

简短实现代码:


代码
 [Serializable]
    
public class SystemConfig : BaseObject<decimal>
    {
        
#region 成员

        
private string _FieldName;
        
private string _FieldValue;
        
private int _FieldGroup;
        
private string _Creator;
        
private DateTime? _CreateDT;
        
private string _Updator;
        
private DateTime? _UpdateDT;

        
#endregion

        
#region 构造函数

        
public SystemConfig()
        { }

        
public SystemConfig(string FieldName, string FieldValue, int FieldGroup, string Creator, DateTime CreateDT, string Updator, DateTime UpdateDT)
        {
            
this._FieldName = FieldName;
            
this._FieldValue = FieldValue;
            
this._FieldGroup = FieldGroup;
            
this._Creator = Creator;
            
this._CreateDT = CreateDT;
            
this._Updator = Updator;
            
this._UpdateDT = UpdateDT;
        }

        
#endregion

        
#region 属性

        
/// <summary>
        
/// 获取与设置字段名称
        
/// </summary>
        public virtual string FieldName
        {
            
get { return _FieldName; }
            
set { _FieldName = value; }
        }
        
#endregion
    }

 

 

 View 层,即*.aspx ,网页,你在设计是,只关注把页面样式调整好,是属于客户端显示的部分。所能看得到的部分。

简短代码如下:


代码
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SystemConfigMVCDemo.Entity.SystemConfig>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        列表
</h2>
    
<link href="http://www.cnblogs.com/ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" />

    
<script src="http://www.cnblogs.com/ext/adapter/ext/ext-base.js" type="text/javascript"></script>
    
<script src="http://www.cnblogs.com/ext/ext-all.js" type="text/javascript"></script>

    
<script src="http://www.cnblogs.com/Content/SystemConfig/SystemConfigAjax.js" type="text/javascript"></script>

    
<script src="http://www.cnblogs.com/Content/SystemConfig/AjaxFrame.js" type="text/javascript"></script>

    
<script src="http://www.cnblogs.com/Content/SystemConfig/SystemConfigForm.js" type="text/javascript"></script>
    
<table>
        
<tr>
            
<th>
            
</th>
            
<th>
                FieldName
            
</th>
            
<th>
                FieldValue
            
</th>
            
<th>
                FieldGroup
            
</th>
            
<th>
                Creator
            
</th>
            
<th>
                CreateDT
            
</th>
            
<th>
                Updator
            
</th>
            
<th>
                UpdateDT
            
</th>
            
<th>
                PkId
            
</th>
            
<th>
                IsChanged
            
</th>
            
<th>
                IsDeleted
            
</th>
        
</tr>
        
<% foreach (var item in Model)
           { 
%>
        
<tr ondblclick="ShowForm(<%=item.PkId %>)">
            
<td>
                
<%= Html.ActionLink("Edit""Edit"new { id = item.PkId,flag = "Edit" })%>|
                
<%= Html.ActionLink("Details""Details"new { id = item.PkId })%>|
                
<%= Html.ActionLink("Delete""Delete"new { id=item.PkId})%>
            
</td>
            
<td>
                
<%= Html.Encode(item.FieldName) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.FieldValue) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.FieldGroup) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.Creator) %>
            
</td>
            
<td>
                
<%= Html.Encode(String.Format("{0:g}", item.CreateDT)) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.Updator) %>
            
</td>
            
<td>
                
<%= Html.Encode(String.Format("{0:g}", item.UpdateDT)) %>
            
</td>
            
<td>
                
<%= Html.Encode(String.Format("{0:F0}", item.PkId)) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.IsChanged) %>
            
</td>
            
<td>
                
<%= Html.Encode(item.IsDeleted) %>
            
</td>
        
</tr>
        
<% } %>
    
</table>
            
<%--<input type="button" id="show-btn" />--%>
    
<div id="hello-win" class="x-hidden">
    
<div id="form"></div>
<%--    <div class="x-window-header">Hello Dialog</div>
    
<div id="hello-tabs">
        
<!-- Auto create tab 1 -->
        
<div class="x-tab" title="Hello World 1">
            
<p>Hello...</p>
        
</div>
        
<!-- Auto create tab 2 -->
        
<div class="x-tab" title="Hello World 2">
            
<p>... World!</p>
        
</div>
    
</div>--%>
</div>
    
<%
    
//int PageCount = 0;
    
//if (Session["PageCount"] != null)
    
//{
    
//    PageCount = int.Parse(Session["PageCount"].ToString());
    
//}
    
//int currendSessionIndex=1;
    ////if(Session["CurrendIndex"]!=null)
    
////{
    
////    currendSessionIndex = int.Parse(Session["CurrendIndex"].ToString());
    
////}
    //currendSessionIndex = int.Parse(hidCurrentIndex.Value.ToString());
     %>
     
<%
         
int PageCount = 0;
         
if (ViewData["PageCount"!= null)
         {
             PageCount 
= int.Parse(ViewData["PageCount"].ToString());
         }
         
int currendSessionIndex = 1;
         
if (ViewData["CurrendIndex"!= null)
         {
             currendSessionIndex 
= int.Parse(ViewData["CurrendIndex"].ToString());
         }         
          
%>
    
<%= Html.ActionLink("首页""index"new { currentIndex =1})%>   <%=Html.ActionLink("上一页""index"new { currentIndex =currendSessionIndex,flag=0})%>   <%=Html.ActionLink("下一页""index"new { currentIndex = currendSessionIndex, flag = 1})%>  <%=Html.ActionLink("尾页""index"new { currentIndex=PageCount,flag=2 })%>

    
<p>
        
<%= Html.ActionLink("Create New""Create"%>
    
</p>
    
<%--<input type="button" id="btnXML" onclick="StartXMLRequest()" />--%>
</asp:Content>

 

Controller

所有你自己写的controller只需继承Controller类即可。他是modelview层之间的桥梁。前一票随笔所提到的service在这里将会使用到。

简单代码:



代码
简单代码:
 
public class SystemConfigController : ControllerBase
    {
        SystemConfigService service 
= new SystemConfigService();
#if unitTest
        
int pageSize=5;
#else 
        
int pageSize = int.Parse(ConfigurationManager.AppSettings["PageSize"].ToString());
#endif
        
//
        
// GET: /SystemConfig/

        
/// <summary>
        
/// SystemConfig列表界面
        
/// </summary>
        
/// <param name="currentIndex">当前页</param>
        
/// <param name="flag">向前还是向后</param>
        
/// <returns>提交界面结果</returns>
        public ActionResult Index(int? currentIndex, int? flag)
        {
            
#region Session 实现
            
//int pageCount = service.GetPageCount(pageSize);
            
//Session["PageCount"] = pageCount;
            
//List<SystemConfig> configList = new List<SystemConfig>();
            
//if (currentIndex.HasValue)
            
//{
            
//    if (currentIndex.Value == 1)
            
//    {
            
//        //第一页,session CurrendIndex置为 1
            
//        if (currentIndex.Value == 1)
            
//        {
            
//            Session["CurrendIndex"] = 1;
            
//        }
            
//    }
            
//}
            
//if (flag.HasValue && currentIndex.HasValue)
            
//{
            
//    //0向前
            
//    if (flag.Value == 0)
            
//    {
            
//        Session["CurrendIndex"] = currentIndex.Value - 1;
            
//        currentIndex = currentIndex.Value - 1;
            
//        configList = service.GetByPage(currentIndex.Value, pageSize);
            
//    }
            
//    //1向后
            
//    else if (flag.Value == 1)
            
//    {
            
//        Session["CurrendIndex"] = currentIndex.Value + 1;
            
//        currentIndex = currentIndex.Value + 1;
            
//        configList = service.GetByPage(currentIndex.Value, pageSize);
            
//    }
            
//    //最后一页
            
//    else if (flag.Value == 2)
            
//    {
            
//        Session["CurrendIndex"] = currentIndex.Value;
            
//        currentIndex = currentIndex.Value;
            
//        configList = service.GetByPage(currentIndex.Value, pageSize);
            
//    }
            
//    if (currentIndex.Value > pageCount)
            
//    {
            
//        configList = service.GetByPage(1, pageSize);
            
//    }
            
//}
            
//else
            
//{
            
//    configList = service.GetByPage(1, pageSize);
            
//}
            
//if (configList == null)
            
//{
            
//    return RedirectToAction("SystemConfigNotFound");
            
//}
            
//else
            
//    return View("Index", configList);
            #endregion

            
#region ViewData实现
            
int pageCount = service.GetPageCount(pageSize);
            ViewData[
"PageCount"= pageCount;
            List
<SystemConfig> configList = new List<SystemConfig>();
            
if (Request.QueryString["currentIndex"]!=null)
               currentIndex 
= int.Parse(Request.QueryString["currentIndex"].ToString());
            
if (Request.QueryString["flag"!= null)
                flag 
= int.Parse(Request.QueryString["flag"].ToString());
            
if (currentIndex.HasValue)
            {
                
if (currentIndex.Value == 1)
                {
                    
//第一页,session CurrendIndex置为 1
                    if (currentIndex.Value == 1)
                    {
                        ViewData[
"CurrendIndex"= 1;
                    }
                }
            }
            
if (flag.HasValue && currentIndex.HasValue)
            {
                
//0向前
                if (flag.Value == 0)
                {
                    ViewData[
"CurrendIndex"= currentIndex.Value - 1;
                    currentIndex 
= currentIndex.Value - 1;
                    
if (currentIndex.Value == 0)
                    {
                        configList 
= service.GetByPage(1, pageSize);
                        ViewData[
"CurrendIndex"= 1;
                    }
                    
else
                        configList 
= service.GetByPage(currentIndex.Value, pageSize);
                }
                
//1向后
                else if (flag.Value == 1)
                {
                    ViewData[
"CurrendIndex"= currentIndex.Value + 1;
                    currentIndex 
= currentIndex.Value + 1;
                    
if (currentIndex.Value >= pageCount)
                    {
                        configList 
= service.GetByPage(pageCount, pageSize);
                        ViewData[
"CurrendIndex"= pageCount;
                    }
                    
else
                     configList 
= service.GetByPage(currentIndex.Value, pageSize);
                }
                
//最后一页
                else if (flag.Value == 2)
                {
                    ViewData[
"CurrendIndex"= currentIndex.Value;
                    currentIndex 
= currentIndex.Value;
                    configList 
= service.GetByPage(currentIndex.Value, pageSize);
                }
            }
            
else
            {
                configList 
= service.GetByPage(1, pageSize);
            }
            
if (configList == null)
            {
                
return RedirectToAction("SystemConfigNotFound");
            }
            
else
                
return View("Index", configList);
            
#endregion
        }

        
/// <summary>
        
/// 获取总页码
        
/// </summary>
        
/// <returns>页大小</returns>
        public int GetPageCount()
        {
            
return service.GetPageCount(pageSize);
        }
    }

 

 

实现要点

所有这些,只需在新建项目时选择asp.netmvc 项目即可,然后在添加自己的controllerviewmodel等。注意的时,在为controller添加view时,只需右键,创建视图即可。

总结

做为微软难得的开源项目,应该拿代码来看看。自己研究研究。