- Rongsen.Com.Cn 版权所有 2008-2010 京ICP备08007000号 京公海网安备11010802026356号 朝阳网安编号:110105199号
 - 北京黑客防线网安工作室-黑客防线网安服务器维护基地为您提供专业的
服务器维护
,企业网站维护
,网站维护
服务 - (建议采用1024×768分辨率,以达到最佳视觉效果) Powered by 黑客防线网安 ©2009-2010 www.rongsen.com.cn
 
 
    
        作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0 | 
二、通用删除操作
分别添加NewsList.aspx和UserList.aspx两个view。
在这两个View中加入删除链接:
<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="News" })%>
和
<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="User" })%>
然后添加一个Controller:
 public ActionResult Delete(string partialName, int? key)
        {
            RepositoryBase repositoryBase = new RepositoryBase(partialName);
            repositoryBase.Delete(key ?? 0);
            return RedirectToAction(partialName + "List");//返回到list
        } 
接下来我们介绍一下RepositoryBase :
   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();
        }
       
    }
 
这里边重点要理解的就是GetBllTypeByName方法。有了这个方法我们就可以动态的通过名字获得相应的Type了。这里还有个问题就是DataContext是从何而来的,我们这里为了简单起见全程声明了一个DataContext没有考虑多线程的情况
public class Context
{
    static DBDataContext context;
    static Context()
    {
        if (context==null)
        {
            context = new DBDataContext();
        }
    }
    public static DBDataContext GetContext()
    {
        return context;
    }
}
 
有个这些当我们想要对一个表进行删除是只要添加相应的链接就可以了(如<%= Html.ActionLink("删除", "Delete", new { key = item.id, partialName="News" })%>)
| 我要申请本站:N点 | 黑客防线官网 | | 
| 专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479 |