ASP.NET MVC教程:数据库表的增删改
2010-09-25 16:18
302 查看
预备知识:
1、了解反射技术
2、了解C#3.0中扩展方法,分布类,Linq to object,Linq to sql
3、了解ASP.NET MVC
在项目中每添加一个表往往都要添加一套增删改代码,而且这些代码很多情况下都很相似,这里我们给出一个通用的解决方案供大家参考。
一、准备工作:
这里我们先要在数据库中添加两个表News和User如下图:然后拖到dbml中生成实体类。
这里我们先准备一个接口:ICommonTable
然后让News和User实体都继承于此接口
二、通用删除操作
分别添加NewsList.aspx和UserList.aspx两个view。
在这两个View中加入删除链接:
和
然后添加一个Controller:
接下来我们介绍一下RepositoryBase :
这里边重点要理解的就是GetBllTypeByName方法。有了这个方法我们就可以动态的通过名字获得相应的Type了。这里还有个问题就是DataContext是从何而来的,我们这里为了简单起见全程声明了一个DataContext没有考虑多线程的情况
有个这些当我们想要对一个表进行删除是只要添加相应的链接就可以了(如<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="News" })%>)
三、通用增加、修改
首先添加一个CreateEditView.aspx视图
然后添加两个Partial视图News.ascx和User.ascx,这两个视图是分别基于News和User类的强类型视图,具体内容参加源码。
接下来我们添加相应的Controller
这里边大家可能有疑问的就是this.UpdateModel(bllTable, true);这个方法在mvc框架中并不存在,这是我添加的扩展方法,这个地方如果使用UpdateModel(bllTable)虽然编译不会报错,但也没有更新成功,查了一下mvc的源码,问题就出在如下源码中:
三、通用增加、修改
首先添加一个CreateEditView.aspx视图
然后添加两个Partial视图News.ascx和User.ascx,这两个视图是分别基于News和User类的强类型视图,具体内容参加源码。
接下来我们添加相应的Controller
这里边大家可能有疑问的就是this.UpdateModel(bllTable, true);这个方法在mvc框架中并不存在,这是我添加的扩展方法,这个地方如果使用UpdateModel(bllTable)虽然编译不会报错,但也没有更新成功,查了一下mvc的源码,问题就出在如下源码中:
三、通用增加、修改
首先添加一个CreateEditView.aspx视图
然后添加两个Partial视图News.ascx和User.ascx,这两个视图是分别基于News和User类的强类型视图,具体内容参加源码。
接下来我们添加相应的Controller
这里边大家可能有疑问的就是this.UpdateModel(bllTable, true);这个方法在mvc框架中并不存在,这是我添加的扩展方法,这个地方如果使用UpdateModel(bllTable)虽然编译不会报错,但也没有更新成功,查了一下mvc的源码,问题就出在如下源码中:
这个typeof(TModel)造成了只会更新声明类型中有的属性,把它换成model.GetType()就可以解决问题了,我扩这的这个方法如下
有了这些,当我们想对新表进行编辑和添加时只需要添加相应的Partial编辑视图就可以了,简化了我们的编程工作。
四、缺点
1、须要按照规则命名,比方说Partial视图需要以相应的类名来命名
2、页面引用是弱类型的
1、了解反射技术
2、了解C#3.0中扩展方法,分布类,Linq to object,Linq to sql
3、了解ASP.NET MVC
在项目中每添加一个表往往都要添加一套增删改代码,而且这些代码很多情况下都很相似,这里我们给出一个通用的解决方案供大家参考。
一、准备工作:
这里我们先要在数据库中添加两个表News和User如下图:然后拖到dbml中生成实体类。
这里我们先准备一个接口:ICommonTable
public interface ICommonTable { int id { get; set; } } |
public partial class News : ICommonTable { } public partial class User : ICommonTable { } |
分别添加NewsList.aspx和UserList.aspx两个view。
在这两个View中加入删除链接:
<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="News" })%> |
<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="User" })%> |
public ActionResult Delete(string partialName, int? key) { RepositoryBase repositoryBase = new RepositoryBase(partialName); repositoryBase.Delete(key ?? 0); return RedirectToAction(partialName + "List");//返回到list } |
public class RepositoryBase { public Type EntityType { get; private set; } public RepositoryBase(string entityType) { Type type = GetBllTypeByName(entityType); EntityType = type; } public ICommonTable CreateNew() { return (ICommonTable)Activator.CreateInstance(EntityType); } /// <summary> /// 通过字符串获得其Type /// </summary> /// <param name="typeName"></param> /// <returns></returns> private static Type GetBllTypeByName(string typeName) { Type type = null; var ass = AppDomain.CurrentDomain.GetAssemblies() .Where(p => p.FullName.Contains("CommonCEDemo")); foreach (var a in ass) { type = a.GetTypes().Where(p => p.Name == typeName).FirstOrDefault(); if (type != null) break; } if (type == null) { throw new Exception("类型未定义:" + typeName); } return type; } public RepositoryBase(Type entityType) { EntityType = entityType; } public ICommonTable Get(int id) { DBDataContext db = Context.GetContext(); return db.GetTable(EntityType).Cast<ICommonTable>().FirstOrDefault(p => p.id == id); } public void Delete(int id) { ICommonTable bllTable = Get(id); Context.GetContext().GetTable(EntityType).DeleteOnSubmit(bllTable); Context.GetContext().SubmitChanges(); } } |
public class Context { static DBDataContext context; static Context() { if (context==null) { context = new DBDataContext(); } } public static DBDataContext GetContext() { return context; } } |
三、通用增加、修改
首先添加一个CreateEditView.aspx视图
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <%Html.RenderPartial(ViewData["PartialName"].ToString()); %> </asp:Content> |
接下来我们添加相应的Controller
public ActionResult CreateEditView(string partialName, int? key) { ViewData["PartialName"] = partialName; RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable table; if (key == null) { table = repositoryBase.CreateNew(); } else { table = repositoryBase.Get(key ?? 0); } return View("CreateEditView", table); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult CreateEditView(string partialName, int? key, FormCollection formCollection) { RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable bllTable; if (key == null) { bllTable = repositoryBase.CreateNew(); } else { bllTable = repositoryBase.Get(key ?? 0); } this.UpdateModel(bllTable, true); if (key == null) { Context.GetContext().GetTable(repositoryBase.EntityType).InsertOnSubmit(bllTable); } Context.GetContext().SubmitChanges(); return RedirectToAction(partialName+"List");//返回到list } |
protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IDictionary<string, ValueProviderResult> valueProvider) where TModel : class { if (model == null) { throw new ArgumentNullException("model"); } if (valueProvider == null) { throw new ArgumentNullException("valueProvider"); } Predicate<string> propertyFilter = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties); IModelBinder binder = Binders.GetBinder(typeof(TModel)); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = prefix, ModelState = ModelState, ModelType = typeof(TModel), PropertyFilter = propertyFilter, ValueProvider = valueProvider }; binder.BindModel(ControllerContext, bindingContext); return ModelState.IsValid; } |
首先添加一个CreateEditView.aspx视图
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <%Html.RenderPartial(ViewData["PartialName"].ToString()); %> </asp:Content> |
接下来我们添加相应的Controller
public ActionResult CreateEditView(string partialName, int? key) { ViewData["PartialName"] = partialName; RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable table; if (key == null) { table = repositoryBase.CreateNew(); } else { table = repositoryBase.Get(key ?? 0); } return View("CreateEditView", table); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult CreateEditView(string partialName, int? key, FormCollection formCollection) { RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable bllTable; if (key == null) { bllTable = repositoryBase.CreateNew(); } else { bllTable = repositoryBase.Get(key ?? 0); } this.UpdateModel(bllTable, true); if (key == null) { Context.GetContext().GetTable(repositoryBase.EntityType).InsertOnSubmit(bllTable); } Context.GetContext().SubmitChanges(); return RedirectToAction(partialName+"List");//返回到list } |
protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IDictionary<string, ValueProviderResult> valueProvider) where TModel : class { if (model == null) { throw new ArgumentNullException("model"); } if (valueProvider == null) { throw new ArgumentNullException("valueProvider"); } Predicate<string> propertyFilter = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties); IModelBinder binder = Binders.GetBinder(typeof(TModel)); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = prefix, ModelState = ModelState, ModelType = typeof(TModel), PropertyFilter = propertyFilter, ValueProvider = valueProvider }; binder.BindModel(ControllerContext, bindingContext); return ModelState.IsValid; } |
首先添加一个CreateEditView.aspx视图
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <%Html.RenderPartial(ViewData["PartialName"].ToString()); %> </asp:Content> |
接下来我们添加相应的Controller
public ActionResult CreateEditView(string partialName, int? key) { ViewData["PartialName"] = partialName; RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable table; if (key == null) { table = repositoryBase.CreateNew(); } else { table = repositoryBase.Get(key ?? 0); } return View("CreateEditView", table); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult CreateEditView(string partialName, int? key, FormCollection formCollection) { RepositoryBase repositoryBase = new RepositoryBase(partialName); ICommonTable bllTable; if (key == null) { bllTable = repositoryBase.CreateNew(); } else { bllTable = repositoryBase.Get(key ?? 0); } this.UpdateModel(bllTable, true); if (key == null) { Context.GetContext().GetTable(repositoryBase.EntityType).InsertOnSubmit(bllTable); } Context.GetContext().SubmitChanges(); return RedirectToAction(partialName+"List");//返回到list } |
protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IDictionary<string, ValueProviderResult> valueProvider) where TModel : class { if (model == null) { throw new ArgumentNullException("model"); } if (valueProvider == null) { throw new ArgumentNullException("valueProvider"); } Predicate<string> propertyFilter = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties); IModelBinder binder = Binders.GetBinder(typeof(TModel)); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = prefix, ModelState = ModelState, ModelType = typeof(TModel), PropertyFilter = propertyFilter, ValueProvider = valueProvider }; binder.BindModel(ControllerContext, bindingContext); return ModelState.IsValid; } |
public static class ControllerExtension { /// <summary> /// 更新时是否按照当前类型进行更新 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="controller"></param> /// <param name="model"></param> /// <param name="isEx"></param> public static void UpdateModel<TModel>(this Controller controller, TModel model, bool isExtension) where TModel : class { if (isExtension) { Predicate<string> propertyFilter = propertyName => IsPropertyAllowed(propertyName, null, null); IModelBinder binder = ModelBinders.Binders.GetBinder(model.GetType()); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = null, ModelState = controller.ModelState, ModelType = model.GetType(), PropertyFilter = propertyFilter, ValueProvider = controller.ValueProvider }; binder.BindModel(controller.ControllerContext, bindingContext); } else { throw new Exception("isExtension不能选择false"); } } private static bool IsPropertyAllowed(string propertyName, string[] includeProperties, string[] excludeProperties) { bool includeProperty = (includeProperties == null) || (includeProperties.Length == 0) || includeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase); bool excludeProperty = (excludeProperties != null) && excludeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase); return includeProperty && !excludeProperty; } } |
四、缺点
1、须要按照规则命名,比方说Partial视图需要以相应的类名来命名
2、页面引用是弱类型的
相关文章推荐
- [ASP.NET MVC2 系列] ASP.Net MVC教程之《在15分钟内用ASP.Net MVC创建一个电影数据库应用程序》
- ASP.NET MVC入门教程(一)项目介绍与数据库的创建
- [翻译:ASP.NET MVC 教程]用ASP.NET MVC 创建一个电影数据库应用程序——补充
- [翻译:ASP.NET MVC 教程]用ASP.NET MVC 创建一个电影数据库应用程序
- 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
- 【EntityFramework系列教程六,翻译】在ASP.NET MVC程序中使用EntityFramework对相关数据进行更新
- ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
- 微软ASP.NET官方网站MVC教程实际操作中的部分问题
- 一起谈.NET技术,将ASP.NET MVC 2.0 部署在IIS6和IIS7上的教程
- Asp.net Mvc 入门视频教程(试水)
- [翻译:ASP.NET MVC 教程]创建自定义路由
- Spring.NET教程(十七)——整合NHibernate和ASP.NET MVC(基础篇)
- ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL&EF
- asp.net MVC + linq to Entity简单教程(二)MVC的简单操作
- asp.net mvc 从数据库中读取图片的实现代码
- Asp.Net Mvc+Localdb数据库项目在IIS部署的配置
- ASP.NET MVC CodeFirst模式数据库迁移步骤
- 【译】ASP.NET MVC 5 教程 - 4:添加模型
- asp.net MVC EFCodeFirst 生成数据库注意事项
- ASP.NET MVC 教程 - 使用实体框架创建模型类(C#)