随着MVC模式在Web应用系统的广泛应用,我们也越来越能体会到MVC模式在Web系统中的应用使后续对程序的修改和扩展变得简单化,并且使程序某一部分的重复利用成为可能。Spread控件不仅可以应用在传统的.NET Web Forms 系统中,当然,Spread在MVC模式的系统中也可以得到完美的应用。下面,我们就通过实例来学习Spread是如何在ASP.NET MVC3系统中得到应用的。

效果截图


实现步骤
1.打开Visual Studio 2010并新建一个ASP.NET MVC 3应用项目。


2.在项目模板窗口选择最适合项目需要的模板和视图引擎,然后单击OK。(在我的示例,我使用了互联网应用程序模板,因为它提供了默认的控制器,视图和一个默认的路由映像)


3.在应用项目中添加2个引用FarPoint.MVC.Spread.dll和FarPoint.Web.Spread.dll,然后重新生成。


4.创建一个数据库,对表数据内容进行填充。


5.创建一个对数据库映射的实体模型。


6.在Index.cshtml声明Mvc命名空间和实体模型
 
@model IEnumerable<SpreadMVC3Applications.Models.User>

@using FarPoint.Mvc.Spread    

复制代码
7.打开HomeController.cs添加如下代码,完成Spread在Mvc中添加、删除以及更新数据的操作。
 
using System; 

using System.Collections.Generic;

using System.Linq; 

using System.Web;

using System.Web.Mvc; 

using SpreadMVC3Applications.Models; 

using FarPoint.Mvc.Spread;

namespace SpreadMVC3Applications.Controllers

{

    public class HomeController : Controller

    {

        UserDBEntities db1 = new UserDBEntities();

        public ActionResult Index()

        {

            return View(db1.User.ToList());

        }

        void User_Load(object sender, EventArgs e)

        {

            FpSpread spread = (FpSpread)sender;

            spread.UpdateCommand += new FarPoint.Web.Spread.SpreadCommandEventHandler(spread_UpdateCommand);

            spread.DeleteCommand += new FarPoint.Web.Spread.SpreadCommandEventHandler(spread_DeleteCommand);

            spread.UpdateCommand +=new FarPoint.Web.Spread.SpreadCommandEventHandler(spread_UpdateCommand);

        }

        void spread_InsertCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)

        {

            FpSpread spread = ((FpSpread)sender); 

            User cs = new Models.User();

            cs.ID = Convert.ToInt32(e.EditValues[0]);

            cs.Name = e.EditValues[1].ToString();

            cs.City = e.EditValues[2].ToString();

            db1.User.AddObject(cs);

            db1.SaveChanges();

            RedirectToAction("Index");

        }

        void spread_DeleteCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)

        {

            FpSpread spread = ((FpSpread)sender);

            int id = (int)e.SheetView.Cells[(int)e.CommandArgument, 0].Value;

            User user = db1.User.Single(c => c.ID == id);

            db1.User.DeleteObject(user);

            db1.SaveChanges();

            spread.SaveChanges();

            RedirectToAction("Index");

        }

        void spread_UpdateCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)

        {

            int id = (int)e.SheetView.Cells[(int)e.CommandArgument, 0].Value;

            User user = db1.User.Single(c => c.ID == id);

            System.Reflection.PropertyInfo[] infos = User.GetType().GetProperties();

            for (int i = 1; i < e.EditValues.Count; i++)

            {

                if (e.EditValues[i].ToString() != "System.Object")

                {

                    infos.Where(a => a.Name == e.SheetView.Columns[i].DataField).ToList<System.Reflection.PropertyInfo>()[0].SetValue(user, e.EditValues[i], null);

                 }

            }

            db1.SaveChanges();

            RedirectToAction("Index");

        }

    }

}

复制代码
8.打开Global.asax.cs, 在Application_Start方法中添加如下代码:
 
FarPoint.Mvc.Spread.MvcSpreadVirtualPathProvider.AppInitialize();

AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);

RegisterRoutes(RouteTable.Routes);

复制代码
9.在Licenses.licx file中添加Spread信息,如下:
 
FarPoint.Web.Spread.FpSpread, FarPoint.Web.Spread, Version=6.0.3505.2008, Culture=neutral, PublicKeyToken=327c3516b1b18457

FarPoint.Mvc.Spread.FpSpread, FarPoint.Mvc.Spread, Version=6.0.4005.2010, Culture=neutral, PublicKeyToken=327c3516b1b18457

复制代码
10.现在我们运行项目就可以看到结果啦。


Demo下载
测试环境:VS 2010 && Spread ASP.NET 6.0
SpreadMVC3Applications.zip (2.16 M, 下载次数:20)