繼我的【Ajax服務端框架】完成后, 也花了些時間學習了一Asp.net MVC,感覺我的Ajax框架也能玩MVC開發, 于是乎,在又加了點功能后,現在也能像Asp.net MVC那樣寫aspx和ascx了。
先來點代碼來看看,具體的頁面呈現效果請參考: 通用數據訪問層及Ajax服務端框架的綜合示例,展示與下載
<%@ Page Title="商品管理" Language="C#" MasterPageFile="~/MasterPage.master" Inherits="FishWebLib.Mvc.MyPageView<ProductsPageData, ProductsModelLoader>" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <script type="text/javascript" src="/js/MyPage/Products.js"></script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table cellpadding="4" border="0"> <tr><td>當前分類</td><td> <select id="ddlCurrentCategory" onchange="<%= UiHelper.DropDownListAutoRedir("ddlCurrentCategory")%>" combobox="not-editable" style="width:300px;"> <%= Model.Categories.ToHtmlOptions("categoryId", false, "searchWord", "page")%> </select> </td><td> <a id="btnCreateItem" href="#" class="easyui-linkbutton" iconCls="icon-add">添加商品</a> </td></tr> </table> <div style="height: 10px"></div> <table class="GridView" cellspacing="0" cellpadding="4" border="0" style="border-collapse:collapse;"> <tr align="left"> <th style="width:20px;"> </th> <th style="width:550px;">商品名稱</th> <th style="width:120px;">單位</th> <th style="width:120px;">單價</th> <th style="width:50px;">數量</th> </tr> <% foreach( Product product in Model.Products ) { %> <tr> <td><a href="/AjaxProduct.Delete.cs?id=<%= product.ProductID %>&returnUrl=<%= this.CurrentRequestRawUrl %>" title="刪除" class="easyui-linkbutton" plain="true"> <img src="/Images/delete.gif" alt="刪除" /></a> </td> <td style="white-space:nowrap;"> <a href="#" rowId="<%= product.ProductID %>" class="easyui-linkbutton" plain="true" iconCls="icon-open" > <%= product.ProductName.HtmlEncode()%></a> </td><td> <span name="Unit"><%= product.Unit.HtmlEncode() %></span> </td><td> <span name="UnitPrice"><%= product.UnitPrice.ToText() %></span> </td><td> <input type="text" pid="<%= product.ProductID %>" value="<%= product.Quantity %>" class="quantityTextbox" /> </td> </tr> <% } %> <%= Model.PagingInfo.PaginationBar(5)%> </table> <input type="hidden" id="hfCurrentCategoryId" value="<%= Model.CurrentCategoryId %>" /> <div id="divProductInfo" title="商品" style="padding: 8px; display: none"> <%= FishWebLib.Ajax.UcExecutor.Execute("~/Controls/ProductInfo.ascx", Model.ProductInfoViewData)%> </div> </asp:Content>
再來個ascx
<%@ Control Language="C#" Inherits="FishWebLib.Mvc.MyUserControlView<OrderListViewData>" %> <table class="GridView" cellspacing="0" cellpadding="4" border="0" style="border-collapse:collapse; width: 99%"> <tr align="left"> <th style="width:100px;">訂單編號</th> <th style="width:160px;">時間</th> <th style="width:300px;">客戶</th> <th style="width:100px;">訂單金額</th> <th style="width:60px;">已處理</th> </tr> <% foreach( var item in Model.List ) { %> <tr> <td> <a href="#" OrderNo="<%= item.OrderID %>" class="easyui-linkbutton" plain="true" iconCls="icon-open"> <%= item.OrderNo %></a> </td> <td><%= string.Format("{0:yyyy-MM-dd HH:mm:ss}", item.OrderDate) %></td> <td> <a href="#" Customer='<%= item.ValidCustomerId %>' class="easyui-linkbutton" plain="true" iconCls="icon-open"> <%= item.CustomerName.HtmlEncode() %></a> </td> <td><%= item.SumMoney.ToText() %></td> <td> <%= item.Finished.ToCheckBox(null, "chk_Finished", true) %> </td> </tr> <% } %> <%= Model.PagingInfo.PaginationBar(5) %> </table>
與Asp.net MVC的aspx,ascx一樣:沒有與之對應的CodeFile
再來看看"商品管理"頁面加載數據的C#代碼吧。注意類型的名稱與方法的簽名,它們在上面Page指令中有引用。
public class ProductsModelLoader { public static ProductsPageData LoadModel() { ProductsPageData result = new ProductsPageData(); result = new ProductsPageData(); result.Categories = BllFactory.GetCategoryBLL().GetList(); if( result.Categories.Count == 0 ) { //HttpContext.Current.Response.Redirect("/Categories.aspx", true); // 不建議使用這個方法,不利于測試 FishWebLib.HttpContextHelper.RequireRedirect("/Categories.aspx"); return null; } // 獲取當前用戶選擇的商品分類ID result.CurrentCategoryId = FishWebLib.FishUrlHelper.GetIntegerFromQueryString("categoryId", 0); if( result.CurrentCategoryId == 0 ) result.CurrentCategoryId = result.Categories[0].CategoryID; result.ProductInfoViewData = new ProductInfoViewData(result.Categories, new Product { CategoryID = result.CurrentCategoryId }); result.PagingInfo.PageIndex = result.PagingInfo.GetPageIndex(); result.PagingInfo.PageSize = AppHelper.DefaultPageSize; // 加載商品列表,并顯示 result.Products = BllFactory.GetProductBLL().GetProductByCategoryId(result.CurrentCategoryId, result.PagingInfo); return result; } } public class ProductsPageData { public PagingInfo PagingInfo; public List<Category> Categories; public int CurrentCategoryId; public List<Product> Products; public ProductInfoViewData ProductInfoViewData { get; set; } public ProductsPageData() { this.PagingInfo = new PagingInfo(); } }
上面那個ascx是供JS調用的,C#的實現代碼如下:(注意是如何執行用戶控件的)
/// <summary> /// 搜索訂單,并以HTML代碼的形式返回給客戶端 /// </summary> /// <returns></returns> public static string Search() { // 從查詢字符串中讀取客戶端發過的日期范圍。 QueryDateRange range = QueryDateRange.GetDateRangeFromQueryString("StartDate", "EndDate"); OrderListViewData data = new OrderListViewData(); data.PagingInfo.PageIndex = data.PagingInfo.GetPageIndex(); data.PagingInfo.PageSize = AppHelper.DefaultPageSize; // 搜索數據庫 data.List = BllFactory.GetOrderBLL().Search(range, data.PagingInfo); // 執行用戶控件,并傳遞所需的呈現數據。 return FishWebLib.Ajax.UcExecutor.Execute("~/Controls/OrderList.ascx", data); } public class OrderListViewData { public List<OrderItem> List; public PagingInfo PagingInfo = new PagingInfo(); }
與Asp.net MVC框架一樣,頁面數據的加載和呈現徹底地分開了。
JS調用上面那個ascx的代碼如下:
function btnQuery_Click(){ var dateRange = GetDateRange("txtStartDate", "txtEndDate"); if( dateRange == null ) return false; var url = '/AjaxOrder.Search.cs?' + $.param({StartDate: dateRange.StartDate, EndDate: dateRange.EndDate}); ShowPickerPage(url, 'divResultList_inner', ShowResultSuccess); return false; }
代碼回顧
學過Asp.net MVC的朋友應該能發現,前面示例代碼中的 aspx, ascx的寫法,與Asp.net MVC的寫法是100%的兼容的。 但我肯定沒有使用Asp.net MVC,您可以下載示例代碼去看。
當初學習Asp.net MVC是因為有些人把Asp.net MVC說得太過于神話了,好像用Asp.net做開發,非它不可。但是,學了之后才發現, 確實,這個東西有很多優點,但沒那么神奇。我認它最出色的地方還是在于它的思想:MVC,這種思想能夠讓我們在開發時, 盡量去分離一些可分離的東西,比如:呈現與獲取數據的過程,從而可以在一個地方只關注一件事情,也就是它所提倡的:分離關注點。
正如前面所說,我認為Asp.net MVC的優點在于它的設計體現了MVC的思想。但讓人不解的是:微軟在實現時,不知為何, 非要把MVC搞得與WebForms差別巨大。這種區別尤其是在加入了Route和一堆Html擴展之后。 對于Route和一堆Html擴展,我對它們沒多大的興趣,因為它們于MVC無關,對于代碼的維護和性能根本沒什么改善。 所以也不打算在這個山寨版本上采用,反而認為現在的代碼更容易理解了。
至于在Ajax方面的實現,我的框架也完全是符合MVC思想的。 而且在生成HTML片段時,仍然可以使用MVC的思想:先準備數據,然后交給ascx來呈現。
我的MVC框架與Asp.net MVC的不同之處:
1. 簡單了很多,拋棄了許多與MVC無關的東西。因此,學習成本更低。
2. Controller少了很多限制,可以在單獨的類庫中實現。為了方便安全檢查,建議放在一個命名空間中,或者取個前綴或后綴,這也符合:約定勝于配置。
3. 由于沒有Route,頁面是由Asp.net框架所調用的,而不是先經過Controller,但頁面的數據加載是由一個輔助的類來完成的,
這個類一定要實現一個名為“LoadModel”的方法(簽名不限,框架會識別),同樣,也是采用約定勝于配置的原則。
Fish Li (李奇峰)
浙公網安備 33010602011771號