使用ASP.NET MVC2+PDF.NET 构建一个简单的新闻管理程序 示例过程

时间:2022-04-29
本文章向大家介绍使用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的详细信息,请看这里