一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

一,摘要

本篇文章我们将使用ASP.NET MVC3,Razor,EF Code First创建Web应用程序.覆盖Unity2.0,泛型Repository以及EF Code First的工作单元.需要使用到的工具如下:

1.ASP.NET MVC 3
2.EF Code First CTP5
3.Unity2.0

二,安装工具

安装好Visual Studio2010后继续安装MVC3(http://www.microsoft.com/downloads/en/details.aspx?FamilyID=d2928bc1-f48c-4e95-a064-2a455a22c8f6),NuGet(http://nuget.codeplex.com/)

三,建立程序

1.打开Visual Studio2010选择选择新建ASP.NET MVC 3 Web Application,取名叫MVC3DIApplication:
一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

点确认后弹出选择框如下:
一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一) 点OK,创建应用程序.

下面添加EF CTP5与Unity2.0,在刚建立的项目上右键,选择添加类库包引用,点击左边的online,在搜索框里输入Unity,找到Unity后点install.,安装成功后(图二)它自动把需要的程序集加入到项目中(图三):
一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

然后继续搜索EFCodeFirst并安装:
一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)

三,建立领域模型

1.添加一个类库,命名MVC3DIApplication.Domain,然后在里面添加文件夹Entities,在文件夹里添加实体模型类
Category.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Web;
<!--CRLF-->
   5: using System.ComponentModel.DataAnnotations;
<!--CRLF-->
   6: 
<!--CRLF-->
   7: namespace MyFinance.Domain
<!--CRLF-->
   8: {
<!--CRLF-->
   9:     public class Category 
<!--CRLF-->
  10:     {
<!--CRLF-->
  11: 
<!--CRLF-->
  12:         public int CategoryId { get; set; }
<!--CRLF-->
  13: 
<!--CRLF-->
  14:         [Required(ErrorMessage = "Name Required")]
<!--CRLF-->
  15:         [StringLength(25, ErrorMessage = "Must be less than 25 characters")]
<!--CRLF-->
  16:         public string Name { get; set;}
<!--CRLF-->
  17:         public string Description { get; set; }
<!--CRLF-->
  18:         public virtual ICollection<expense> Expenses { get; set; }</expense>
<!--CRLF-->
  19:     }
<!--CRLF-->
  20: }
<!--CRLF-->

Expense.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: 
<!--CRLF-->
   6: namespace MVC3DIApplication.Domain.Entities
<!--CRLF-->
   7: {
<!--CRLF-->
   8:     public class Expense
<!--CRLF-->
   9:     {
<!--CRLF-->
  10:         public int ExpenseId { get; set; }
<!--CRLF-->
  11:         public string Transaction { get; set; }
<!--CRLF-->
  12:         public DateTime Date { get; set; }
<!--CRLF-->
  13:         public double Amount { get; set; }
<!--CRLF-->
  14:         public int CategoryId { get; set; }
<!--CRLF-->
  15:         public virtual Category Category { get; set; }
<!--CRLF-->
  16:     }
<!--CRLF-->
  17: }
<!--CRLF-->


2.继续添加类库MVC3DIApplication.Data,在其下新建一个叫MyFinanceContext的类,该类继承自DbContext,用来映射我们的模型到数据库表

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: using System.Data.Entity;
<!--CRLF-->
   6: using MVC3DIApplication.Domain.Entities;
<!--CRLF-->
   7: 
<!--CRLF-->
   8: namespace MVC3DIApplication.Data
<!--CRLF-->
   9: {
<!--CRLF-->
  10:     public class MyFinanceContext : DbContext
<!--CRLF-->
  11:     {
<!--CRLF-->
  12:         public MyFinanceContext() : base("MyFinance") { }
<!--CRLF-->
  13:         public DbSet<category> Categories { get; set; }</category>
<!--CRLF-->
  14:         public DbSet<expense> Expenses { get; set; }</expense>
<!--CRLF-->
  15:         public virtual void Commit()
<!--CRLF-->
  16:         {
<!--CRLF-->
  17:             base.SaveChanges();
<!--CRLF-->
  18:         }
<!--CRLF-->
  19:     }
<!--CRLF-->
  20: }
<!--CRLF-->

3.修改Web.comfig:

   1: connectionStrings>
<!--CRLF-->
   2:       add name="MyFinance" connectionString="data source=./MSSQLSERVER2008;Initial Catalog=MVC3DI;Persist Security Info=True;User ID=sa;Password=suzhi921;" providerName="System.Data.SqlClient" />
<!--CRLF-->
   3:       add name="ApplicationServices" connectionString="data source=./SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<!--CRLF-->
   4:     connectionStrings>
<!--CRLF-->

四,建立泛型Repository

MVC3DIApplication.Data类库下建立Infrastructure文件夹,在里面添加类IRepository.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: 
<!--CRLF-->
   6: namespace MVC3DIApplication.Data.Infrastructure
<!--CRLF-->
   7: {
<!--CRLF-->
   8:     public interface IRepository<t><span style="color: #0000ff">where</span> T : <span style="color: #0000ff">class</span></t>
<!--CRLF-->
   9:     {
<!--CRLF-->
  10:         void Add(T entity);
<!--CRLF-->
  11:         void Delete(T entity);
<!--CRLF-->
  12:         void Delete(Func<t boolean> predicate);</t>
<!--CRLF-->
  13:         T GetById(long Id);
<!--CRLF-->
  14:         T Get(Func<t boolean><span style="color: #0000ff">where</span>);</t>
<!--CRLF-->
  15:         IEnumerable<t> GetAll();</t>
<!--CRLF-->
  16:         IEnumerable<t> GetMany(Func<t style="color: #0000ff">bool&gt; <span style="color: #0000ff">where</span>);</t></t>
<!--CRLF-->
  17:     }
<!--CRLF-->
  18: }
<!--CRLF-->

IDatabaseFactory.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: 
<!--CRLF-->
   6: namespace MVC3DIApplication.Data.Infrastructure
<!--CRLF-->
   7: {
<!--CRLF-->
   8:     public interface IDatabaseFactory : IDisposable
<!--CRLF-->
   9:     {
<!--CRLF-->
  10:         MyFinanceContext Get();
<!--CRLF-->
  11:     }
<!--CRLF-->
  12: }
<!--CRLF-->

RepositoryBase.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: using System.Data.Entity;
<!--CRLF-->
   6: 
<!--CRLF-->
   7: namespace MVC3DIApplication.Data.Infrastructure
<!--CRLF-->
   8: {
<!--CRLF-->
   9:     public abstract class RepositoryBase<t><span style="color: #0000ff">where</span> T : <span style="color: #0000ff">class</span></t>
<!--CRLF-->
  10:     {
<!--CRLF-->
  11:         private MyFinanceContext dataContext;
<!--CRLF-->
  12:         private readonly IDbSet<t> dbset;</t>
<!--CRLF-->
  13:         protected RepositoryBase(IDatabaseFactory databaseFactory)
<!--CRLF-->
  14:         {
<!--CRLF-->
  15:             DatabaseFactory = databaseFactory;
<!--CRLF-->
  16:             dbset = DataContext.Set<t>();</t>
<!--CRLF-->
  17:         }
<!--CRLF-->
  18: 
<!--CRLF-->
  19:         protected IDatabaseFactory DatabaseFactory
<!--CRLF-->
  20:         {
<!--CRLF-->
  21:             get;
<!--CRLF-->
  22:             private set;
<!--CRLF-->
  23:         }
<!--CRLF-->
  24: 
<!--CRLF-->
  25:         protected MyFinanceContext DataContext
<!--CRLF-->
  26:         {
<!--CRLF-->
  27:             get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
<!--CRLF-->
  28:         }
<!--CRLF-->
  29:         public virtual void Add(T entity)
<!--CRLF-->
  30:         {
<!--CRLF-->
  31:             dbset.Add(entity);
<!--CRLF-->
  32:         }
<!--CRLF-->
  33: 
<!--CRLF-->
  34:         public virtual void Delete(T entity)
<!--CRLF-->
  35:         {
<!--CRLF-->
  36:             dbset.Remove(entity);
<!--CRLF-->
  37:         }
<!--CRLF-->
  38:         public void Delete(Func<t boolean><span style="color: #0000ff">where</span>)</t>
<!--CRLF-->
  39:         {
<!--CRLF-->
  40:             IEnumerable<t> objects = dbset.Where<t>(<span style="color: #0000ff">where</span>).AsEnumerable();</t></t>
<!--CRLF-->
  41:             foreach (T obj in objects)
<!--CRLF-->
  42:                 dbset.Remove(obj);
<!--CRLF-->
  43:         }
<!--CRLF-->
  44:         public virtual T GetById(long id)
<!--CRLF-->
  45:         {
<!--CRLF-->
  46:             return dbset.Find(id);
<!--CRLF-->
  47:         }
<!--CRLF-->
  48: 
<!--CRLF-->
  49:         public virtual IEnumerable<t> GetAll()</t>
<!--CRLF-->
  50:         {
<!--CRLF-->
  51:             return dbset.ToList();
<!--CRLF-->
  52:         }
<!--CRLF-->
  53:         public virtual IEnumerable<t> GetMany(Func<t style="color: #0000ff">bool&gt; <span style="color: #0000ff">where</span>)</t></t>
<!--CRLF-->
  54:         {
<!--CRLF-->
  55:             return dbset.Where(where).ToList();
<!--CRLF-->
  56:         }
<!--CRLF-->
  57:         public T Get(Func<t boolean><span style="color: #0000ff">where</span>)</t>
<!--CRLF-->
  58:         {
<!--CRLF-->
  59:             return dbset.Where(where).FirstOrDefault<t>();</t>
<!--CRLF-->
  60:         }
<!--CRLF-->
  61:     }
<!--CRLF-->
  62: }
<!--CRLF-->

五,工作单元

工作单元主要用来维护受Business transaction影响的对象以及并发等问题
MVC3DIApplication.Data类库的Infrastructure下建立IUnitOfWork.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: 
<!--CRLF-->
   6: namespace MVC3DIApplication.Data.Infrastructure
<!--CRLF-->
   7: {
<!--CRLF-->
   8:     public interface IUnitOfWork
<!--CRLF-->
   9:     {
<!--CRLF-->
  10:         void Commit();
<!--CRLF-->
  11:     }
<!--CRLF-->
  12: }
<!--CRLF-->

UnitOfWork.cs:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: 
<!--CRLF-->
   6: namespace MVC3DIApplication.Data.Infrastructure
<!--CRLF-->
   7: {
<!--CRLF-->
   8:     public class UnitOfWork : IUnitOfWork
<!--CRLF-->
   9:     {
<!--CRLF-->
  10:         private readonly IDatabaseFactory databaseFactory;
<!--CRLF-->
  11:         private MyFinanceContext dataContext;
<!--CRLF-->
  12: 
<!--CRLF-->
  13:         public UnitOfWork(IDatabaseFactory databaseFactory)
<!--CRLF-->
  14:         {
<!--CRLF-->
  15:             this.databaseFactory = databaseFactory;
<!--CRLF-->
  16:         }
<!--CRLF-->
  17: 
<!--CRLF-->
  18:         protected MyFinanceContext DataContext
<!--CRLF-->
  19:         {
<!--CRLF-->
  20:             get { return dataContext ?? (dataContext = databaseFactory.Get()); }
<!--CRLF-->
  21:         }
<!--CRLF-->
  22: 
<!--CRLF-->
  23:         public void Commit()
<!--CRLF-->
  24:         {
<!--CRLF-->
  25:             DataContext.Commit();
<!--CRLF-->
  26:         }
<!--CRLF-->
  27:     }
<!--CRLF-->
  28: }
<!--CRLF-->

六,Category Repository

MVC3DIApplication.Data类库的下建立Repositories文件夹,里面存储我们的Repositories,下面简历CategoryRepository:

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Text;
<!--CRLF-->
   5: using MVC3DIApplication.Data.Infrastructure;
<!--CRLF-->
   6: using MVC3DIApplication.Domain.Entities;
<!--CRLF-->
   7: 
<!--CRLF-->
   8: namespace MVC3DIApplication.Data.Repositories
<!--CRLF-->
   9: {
<!--CRLF-->
  10:     public class CategoryRepository : RepositoryBase<category>, ICategoryRepository</category>
<!--CRLF-->
  11:     {
<!--CRLF-->
  12:         public CategoryRepository(IDatabaseFactory databaseFactory)
<!--CRLF-->
  13:             : base(databaseFactory)
<!--CRLF-->
  14:         {
<!--CRLF-->
  15:         }
<!--CRLF-->
  16:     }
<!--CRLF-->
  17:     public interface ICategoryRepository : IRepository<category></category>
<!--CRLF-->
  18:     {
<!--CRLF-->
  19:     }
<!--CRLF-->
  20: }
<!--CRLF-->

如果我们有额外的方法,那么我们能定义在上面的Repository里.

七,使用Unity2.0

我们为Unity创建一个自定义的生命周期管理器去存储当前上下文的容器,同时创建控制器工厂

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Web;
<!--CRLF-->
   5: using Microsoft.Practices.Unity;
<!--CRLF-->
   6: using System.Web.Mvc;
<!--CRLF-->
   7: using System.Web.Routing;
<!--CRLF-->
   8: 
<!--CRLF-->
   9: namespace MVC3DIApplication.IoC
<!--CRLF-->
  10: {
<!--CRLF-->
  11:     public class UnityControllerFactory : DefaultControllerFactory
<!--CRLF-->
  12:     {
<!--CRLF-->
  13:         IUnityContainer container;
<!--CRLF-->
  14:         public UnityControllerFactory(IUnityContainer container)
<!--CRLF-->
  15:         {
<!--CRLF-->
  16:             this.container = container;
<!--CRLF-->
  17:         }
<!--CRLF-->
  18:         protected override IController GetControllerInstance(RequestContext reqContext, Type controllerType)
<!--CRLF-->
  19:         {
<!--CRLF-->
  20:             IController controller;
<!--CRLF-->
  21:             if (controllerType == null)
<!--CRLF-->
  22:                 throw new HttpException(
<!--CRLF-->
  23:                         404, String.Format(
<!--CRLF-->
  24:                             "The controller for path '{0}' could not be found" +
<!--CRLF-->
  25:             "or it does not implement IController.",
<!--CRLF-->
  26:                         reqContext.HttpContext.Request.Path));
<!--CRLF-->
  27: 
<!--CRLF-->
  28:             if (!typeof(IController).IsAssignableFrom(controllerType))
<!--CRLF-->
  29:                 throw new ArgumentException(
<!--CRLF-->
  30:                         string.Format(
<!--CRLF-->
  31:                             "Type requested is not a controller: {0}",
<!--CRLF-->
  32:                             controllerType.Name),
<!--CRLF-->
  33:                             "controllerType");
<!--CRLF-->
  34:             try
<!--CRLF-->
  35:             {
<!--CRLF-->
  36:                 //controller = MvcUnityContainer.Container.Resolve(controllerType)
<!--CRLF-->
  37:                 //                as IController;
<!--CRLF-->
  38:                 controller = container.Resolve(controllerType) as IController;
<!--CRLF-->
  39:             }
<!--CRLF-->
  40:             catch (Exception ex)
<!--CRLF-->
  41:             {
<!--CRLF-->
  42:                 throw new InvalidOperationException(String.Format(
<!--CRLF-->
  43:                                         "Error resolving controller {0}",
<!--CRLF-->
  44:                                         controllerType.Name), ex);
<!--CRLF-->
  45:             }
<!--CRLF-->
  46:             return controller;
<!--CRLF-->
  47:         }
<!--CRLF-->
  48: 
<!--CRLF-->
  49:     }
<!--CRLF-->
  50:     public class HttpContextLifetimeManager<t> : LifetimeManager, IDisposable</t>
<!--CRLF-->
  51:     {
<!--CRLF-->
  52:         public override object GetValue()
<!--CRLF-->
  53:         {
<!--CRLF-->
  54:             return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
<!--CRLF-->
  55:         }
<!--CRLF-->
  56:         public override void RemoveValue()
<!--CRLF-->
  57:         {
<!--CRLF-->
  58:             HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
<!--CRLF-->
  59:         }
<!--CRLF-->
  60:         public override void SetValue(object newValue)
<!--CRLF-->
  61:         {
<!--CRLF-->
  62:             HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
<!--CRLF-->
  63:         }
<!--CRLF-->
  64:         public void Dispose()
<!--CRLF-->
  65:         {
<!--CRLF-->
  66:             RemoveValue();
<!--CRLF-->
  67:         }
<!--CRLF-->
  68:     }
<!--CRLF-->
  69: }
<!--CRLF-->

配置Unity:

   1: protected void Application_Start()
<!--CRLF-->
   2:        {
<!--CRLF-->
   3:            AreaRegistration.RegisterAllAreas();
<!--CRLF-->
   4:            RegisterGlobalFilters(GlobalFilters.Filters);
<!--CRLF-->
   5:            RegisterRoutes(RouteTable.Routes);
<!--CRLF-->
   6:            IUnityContainer container = GetUnityContainer();
<!--CRLF-->
   7:            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
<!--CRLF-->
   8:        }
<!--CRLF-->
   9: 
<!--CRLF-->
  10:        private IUnityContainer GetUnityContainer()
<!--CRLF-->
  11:        {
<!--CRLF-->
  12:            //Create UnityContainer          
<!--CRLF-->
  13:            IUnityContainer container = new UnityContainer()
<!--CRLF-->
  14:            .RegisterType<idatabasefactory databasefactory>(<span style="color: #0000ff">new</span> HttpContextLifetimeManager<idatabasefactory>())</idatabasefactory></idatabasefactory>
<!--CRLF-->
  15:            .RegisterType<iunitofwork unitofwork>(<span style="color: #0000ff">new</span> HttpContextLifetimeManager<iunitofwork>())</iunitofwork></iunitofwork>
<!--CRLF-->
  16:            .RegisterType<icategoryrepository categoryrepository>(<span style="color: #0000ff">new</span> HttpContextLifetimeManager<icategoryrepository>());</icategoryrepository></icategoryrepository>
<!--CRLF-->
  17:            return container;
<!--CRLF-->
  18:        }
<!--CRLF-->

八,建立控制器

   1: using System;
<!--CRLF-->
   2: using System.Collections.Generic;
<!--CRLF-->
   3: using System.Linq;
<!--CRLF-->
   4: using System.Web;
<!--CRLF-->
   5: using System.Web.Mvc;
<!--CRLF-->
   6: using MyFinance.Data;
<!--CRLF-->
   7: using MyFinance.Data.Infrastructure;
<!--CRLF-->
   8: using MyFinance.Domain;
<!--CRLF-->
   9: using MyFinance.Helpers;
<!--CRLF-->
  10: using MyFinance.Service;
<!--CRLF-->
  11: namespace MyFinance.Web.Controllers
<!--CRLF-->
  12: {
<!--CRLF-->
  13:   
<!--CRLF-->
  14: public class CategoryController : Controller
<!--CRLF-->
  15: {
<!--CRLF-->
  16:     private readonly ICategoryRepository categoryRepository;
<!--CRLF-->
  17:     private readonly IUnitOfWork unitOfWork;
<!--CRLF-->
  18:  
<!--CRLF-->
  19:         public CategoryController(ICategoryRepository categoryRepository, IUnitOfWork unitOfWork)
<!--CRLF-->
  20:     {
<!--CRLF-->
  21:      this.categoryRepository = categoryRepository;
<!--CRLF-->
  22:         this.unitOfWork = unitOfWork;
<!--CRLF-->
  23:     }  
<!--CRLF-->
  24:     public ActionResult Index()
<!--CRLF-->
  25:     {
<!--CRLF-->
  26:         var categories = categoryRepository.GetAll();
<!--CRLF-->
  27:         return View(categories);
<!--CRLF-->
  28:     }
<!--CRLF-->
  29:     [HttpGet]
<!--CRLF-->
  30:     public ActionResult Edit(int id)
<!--CRLF-->
  31:     {
<!--CRLF-->
  32:         var category = categoryRepository.GetById(id);
<!--CRLF-->
  33:         return View(category);
<!--CRLF-->
  34:     }
<!--CRLF-->
  35:  
<!--CRLF-->
  36:     [HttpPost]
<!--CRLF-->
  37:     public ActionResult Edit(int id, FormCollection collection)
<!--CRLF-->
  38:     {
<!--CRLF-->
  39:         var category = categoryRepository.GetById(id);
<!--CRLF-->
  40:         if (TryUpdateModel(category))
<!--CRLF-->
  41:         {
<!--CRLF-->
  42:             unitOfWork.Commit();
<!--CRLF-->
  43:             return RedirectToAction("Index");
<!--CRLF-->
  44:         }
<!--CRLF-->
  45:         else return View(category);            
<!--CRLF-->
  46:     } 
<!--CRLF-->
  47:  
<!--CRLF-->
  48:     [HttpGet]
<!--CRLF-->
  49:     public ActionResult Create()
<!--CRLF-->
  50:     {
<!--CRLF-->
  51:         var category = new Category();
<!--CRLF-->
  52:         return View(category);
<!--CRLF-->
  53:     }
<!--CRLF-->
  54:      
<!--CRLF-->
  55:     [HttpPost]
<!--CRLF-->
  56:     public ActionResult Create(Category category)
<!--CRLF-->
  57:     {
<!--CRLF-->
  58:         if (!ModelState.IsValid)
<!--CRLF-->
  59:         {
<!--CRLF-->
  60:             return View("Create", category);
<!--CRLF-->
  61:         }            
<!--CRLF-->
  62:         categoryRepository.Add(category); 
<!--CRLF-->
  63:         unitOfWork.Commit();
<!--CRLF-->
  64:         return RedirectToAction("Index");
<!--CRLF-->
  65:     }
<!--CRLF-->
  66:  
<!--CRLF-->
  67:     [HttpPost]
<!--CRLF-->
  68:     public ActionResult Delete(int  id)
<!--CRLF-->
  69:    {
<!--CRLF-->
  70:         var category = categoryRepository.GetById(id);
<!--CRLF-->
  71:         categoryRepository.Delete(category);
<!--CRLF-->
  72:         unitOfWork.Commit();
<!--CRLF-->
  73:        var categories = categoryRepository.GetAll();
<!--CRLF-->
  74:        return PartialView("CategoryList", categories);
<!--CRLF-->
  75:  
<!--CRLF-->
  76:     }       
<!--CRLF-->
  77: }
<!--CRLF-->
  78: }
<!--CRLF-->

九,建立视图

CategoryList.cshtml:

   1: @using MVC3DIApplication.Domain.Entities;
<!--CRLF-->
   2: @model IEnumerableCategory>   
<!--CRLF-->
   3:   table>
<!--CRLF-->
   4:         tr> 
<!--CRLF-->
   5:         th>Actionsth>
<!--CRLF-->
   6:         th>Nameth>
<!--CRLF-->
   7:          th>Descriptionth>
<!--CRLF-->
   8:         tr>
<!--CRLF-->
   9:     @foreach (var item in Model) {    
<!--CRLF-->
  10:         tr>
<!--CRLF-->
  11:             td>
<!--CRLF-->
  12:                 @Html.ActionLink("Edit", "Edit",new { id = item.CategoryId })
<!--CRLF-->
  13:                 @Ajax.ActionLink("Delete", "Delete", new { id = item.CategoryId },
<!--CRLF-->
  14:                 new AjaxOptions { Confirm = "Delete Expense?", HttpMethod = "Post", 
<!--CRLF-->
  15:                     UpdateTargetId = "divCategoryList" })              
<!--CRLF-->
  16:             td>
<!--CRLF-->
  17:             td>
<!--CRLF-->
  18:                 @item.Name
<!--CRLF-->
  19:             td>
<!--CRLF-->
  20:             td>
<!--CRLF-->
  21:                 @item.Description
<!--CRLF-->
  22:             td>
<!--CRLF-->
  23:         tr>
<!--CRLF-->
  24:     
<!--CRLF-->
  25:     }
<!--CRLF-->
  26: 
<!--CRLF-->
  27:     table>
<!--CRLF-->
  28:     p>
<!--CRLF-->
  29:         @Html.ActionLink("Create New", "Create")
<!--CRLF-->
  30:     p>
<!--CRLF-->
Index.cshtml:
   1: @model IEnumerableMVC3DIApplication.Domain.Entities.Category>
<!--CRLF-->
   2: @{
<!--CRLF-->
   3:     ViewBag.Title = "Index";
<!--CRLF-->
   4: }
<!--CRLF-->
   5: h2>Category Listh2>   
<!--CRLF-->
   6: script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript">script>   
<!--CRLF-->
   7:    div id="divCategoryList">          
<!--CRLF-->
   8:     @Html.Partial("CategoryList", Model)
<!--CRLF-->
   9: div>
<!--CRLF-->

Create.cshtml:

   1: @model MVC3DIApplication.Domain.Entities.Category
<!--CRLF-->
   2: 
<!--CRLF-->
   3: @{
<!--CRLF-->
   4:     ViewBag.Title = "Create";
<!--CRLF-->
   5: }
<!--CRLF-->
   6: 
<!--CRLF-->
   7: h2>Createh2>
<!--CRLF-->
   8: 
<!--CRLF-->
   9: script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">script>
   1: 
<!--CRLF-->
   2: <script src="&lt;span" style="color: #006080">"@Url.Content("</span>~/Scripts/jquery.validate.unobtrusive.min.js<span style="color: #006080">")"</span> type=<span style="color: #006080">"text/javascript"</span>></pre><!--CRLF--><span style="color: #0000ff"></</span><span style="color: #800000">script</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> @using (Html.BeginForm()) {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>     @Html.ValidationSummary(true)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     <span style="color: #0000ff"><</span><span style="color: #800000">fieldset</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         <span style="color: #0000ff"><</span><span style="color: #800000">legend</span><span style="color: #0000ff">></span>Category<span style="color: #0000ff"></</span><span style="color: #800000">legend</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>                @Html.EditorFor(model =<span style="color: #0000ff">></span> model)      </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         <span style="color: #0000ff"><</span><span style="color: #800000">p</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>             <span style="color: #0000ff"><</span><span style="color: #800000">input</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">="submit"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="Create"</span> <span style="color: #0000ff">/></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>         <span style="color: #0000ff"></</span><span style="color: #800000">p</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>     <span style="color: #0000ff"></</span><span style="color: #800000">fieldset</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> <span style="color: #0000ff"><</span><span style="color: #800000">div</span><span style="color: #0000ff">></span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     @Html.ActionLink("Back to List", "Index")</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span> <span style="color: #0000ff"></</span><span style="color: #800000">div</span><span style="color: #0000ff">></span></pre>
<!--CRLF--></div>
</div>
以及共享视图的启动文件_Layout.cshtml

<p></p>

<div id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 10px; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> @{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 12px; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     Layout = "~/Views/Shared/_Layout.cshtml";</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> }</pre>
<!--CRLF--></div>
</div>

<p><font size="2"><font face="宋体"><font size="4"><strong>十,注意事项</strong></font></font></font></p>

<p><font size="2" face="宋体">上面代码还没进行重构,源码稍后以论坛留言的形式提供,由于时间仓促写的难免有很多错,请见谅.欢迎您请继续关注下文,谢谢您的访问.</font></p>
</div>
<div class="share_buttons" id="sharePanel"></div>

<div class="article_next_prev">
        <li class="prev_article">
            <span>上一篇:</span><a href="http://blog.csdn.net/suzhi921/article/details/6184970">WCF4.0(一)</a></li>
        <li class="next_article">
            <span>下一篇:</span><a href="http://blog.csdn.net/suzhi921/article/details/6192097">一步一步学习ASP.NET MVC3 &amp;EF Code First CTP 5&amp;DI(二)</a></li>
</div>


</div>
<div id="ad_cen"></div>
<script type="text/javascript">
    new Ad(4, 'ad_cen');
</script>
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
<script type="text/javascript"> var fileName = '6188768'; var commentscount = 10; var islock = false </script><script type="text/javascript" src="http://static.blog.csdn.net/scripts/comment.js"></script>
<script type="text/javascript"> new Ad(5, 'ad_bot'); </script>
个人资料
一步一步学习ASP.NET MVC3 &EF Code First CTP 5&DI(一)
suzhi921
  • 访问:8360次
  • 积分:386分
  • 排名:第18700名
  • 原创:28篇
  • 转载:0篇
  • 译文:1篇
  • 评论:18条
文章搜索
文章分类