1. 程式人生 > 其它 >使用ASP.NET MVC2+PDF.NET 構建一個簡單的新聞管理程式 示例過程

使用ASP.NET MVC2+PDF.NET 構建一個簡單的新聞管理程式 示例過程

     最近開始學習ASP.NET MVC技術,感覺跟原來的ASP.NET WebForm差異實在是太大了,看了2天的理論知識,才敢動手寫一個例項程式。儘管是看的網上手把手的教程,如果不自己動手實踐的話還是很難正真理解什麼是MVC。     在強調更好的Web使用者體驗前提下,程式設計師需要將UI的主要工作讓步於美工設計人員,程式設計師的主要工作主要關注與後臺邏輯開發,這種開發模式下,MVC無疑是最佳選擇。     ASP.NET MVC 已經從Ver1.0 升級到現在的Ver3.0了。我們的VS2010中預設集成了ASP.NET MVC2.0 ,本例程式就是使用它開發的,例項程式下載請到下面的下載地址:

http://files.cnblogs.com/bluedoctor/MvcApplication1.rar

    由於是我第一次寫MVC程式,所以同樣適合對MVC想入門的朋友,另外,資料訪問採用了PDF.NET資料開發框架,使得程式非常簡單。為方便上傳,示例程式刪除了ASP.NET自帶的成員資料庫,對“新聞”程式的執行沒有影響。

下面是本例項程式的執行截圖: (列表介面)

(帶一個jQuery 日曆控制元件的編輯介面)

(檢視詳細介面)

示例程式的使用 1,首先,下載本例項程式,在VS2010中開啟; 2,開啟SQLSERVER企業管理器,找一個數據庫,然後執行下面的建立表的指令碼語句:

CREATE TABLE [Tb_News](   
[ID] [int] IDENTITY(1,1)  Primary Key NOT NULL,   
[Title] [varchar](100) NOT NULL,   
[CreateTime] [datetime] NULL,   
[Content] [varchar](2000) NULL,   
 )

3,在VS2010中開啟Web.config檔案,看到下面的配置節:

<connectionStrings>  
<add name="ApplicationServices"  
connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"  
providerName="System.Data.SqlClient" />  
<add name="default" connectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" providerName="SqlServer"/>  
</connectionStrings>

修改 name="default" 相關的 connectionString 配置中的內容,為你第2步中建立表的資料庫所在的連線字串。

providerName="SqlServer"  為PDF.NET資料開發框架的資料提供程式。 4,現在就可以按F5執行程式了,能夠看到上面的執行介面。

示例過程

有關ASP.NET MVC的原理和“手把手”的示例過程,請看下面的部落格,寫得非常清楚:Asp.net MVC2.0系列文章http://www.cnblogs.com/ywqu/category/250787.html本例項程式使用了PDF.NET 資料開發框架,新聞資料的增,刪,改,查非常簡單,下面是例項程式碼:

using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Web;   
using MvcApplication1.Models;   
using PWMIS.DataMap.Entity;   
namespace MvcApplication1.Service   
{   
public class NewsService   
{   
public static bool AddNews(AddNewsModel news)   
{   
NewsModel nm = new NewsModel();   
nm.ID = 0;   
nm.Content = news.Content;   
nm.Title = news.Title;   
nm.CreateTime = news.CreateTime;   
EntityQuery<NewsModel> q = new EntityQuery<NewsModel>();   
q.Insert(nm);   
return nm.ID>0;   
}   
public static List<NewsModel> GetAllNews()   
{   
OQL q = OQL.From(new NewsModel()).Select().END;   
return EntityQuery<NewsModel>.QueryList(q);   
}   
public static NewsModel GetNews(int newsID)   
{   
NewsModel nm = new NewsModel();   
nm.ID = newsID;   
EntityQuery<NewsModel>.Fill(nm);   
return nm;   
}   
public static bool EditNews(NewsModel news)   
{   
EntityQuery<NewsModel> q = new EntityQuery<NewsModel>();   
int count=q.Update(news);   
return count > 0;   
}   
public static bool DeleteNews(NewsModel news)   
{   
EntityQuery<NewsModel> q = new EntityQuery<NewsModel>();   
int count = q.Delete(news);   
return count > 0;   
}   
}   
}

由於操作資料非常簡單,我就不詳細說明了,有關PDF.NET的詳細資訊,請看這裡