如何在使用ASP.NET MVC导入Excel之前删除特定表中的所有记录

时间:2017-10-02 21:51:58

标签: asp.net-mvc

请在保存新记录之前,如何删除所有现有记录,刷新并重置表格。我想从我的控制器那里做到。

控制器

        public ActionResult Create(ITEM items)
    {
        try
        {
            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {

                _itemsService.AddCity(thirdparties);
                return RedirectToAction("Index");
            }
        }
        catch
        {
            ModelState.AddModelError("", "We cannot add this items. Verify your data entries !");
        }

        return View(items);
    }

我有一个N层图层

  1. 基础设施
  2. 存储库
  3. 网络(呈现
  4. 基础设施

        public interface IRepository<T> where T : class
        {
        IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate = null);
        T FindById(object id);
        T FindByEntity(T entity);
        T FindByString(object strValue);
        void Add(T entity);
        void Remove(T entity);
       }
    
    
        public abstract class Repository<T> : IRepository<T> where T : class
    {
        public virtual IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate = null)
        {
            IQueryable<T> items = DataContextFactory.AdminEntities.Set<T>();
            return (predicate == null) ? items : items.Where(predicate);
        }
    
        public abstract T FindById(object id);
    
        public abstract T FindByString(object strValue);
    
        public abstract T FindByEntity(T entity);
    
        public virtual void Add(T entity)
        {
            DataContextFactory.AdminEntities.Set<T>().AddOrUpdate(entity);
    
            DataContextFactory.AdminEntities.SaveChanges();
        }
    
        public virtual void Remove(T entity)
        {
            T item = FindByEntity(entity);
    
            if (item != null)
                DataContextFactory.AdminEntities.Set<T>().Remove(item);
    
            DataContextFactory.AdminEntities.SaveChanges();
        }
    
    
    }
    

    存储库

        public class CitiesManager: ICitiesManager
    {
        private readonly IRepository<CITIES> _citiesRepository;
    
        public CitiesManager(IRepository<CITIES> citiesRepository)
        {
            _citiesRepository = citiesRepository;
        }
    
        public CITIES City(Int32 cityID)
        {
            return _citiesRepository.FindById(cityID);
        }
    
        public IEnumerable<CITIES> Cities()
        {
            return _citiesRepository.FindAll().ToList();
        }
    
        public void AddCity(CITIES cities)
        {
            _citiesRepository.Add(cities);
        }
    
        public void RemoveCity(Int32 cityID)
        {
            _citiesRepository.Remove(City(cityID));
        }
    }
    

    我的模型类属于基础设施

    基础设施 - &gt;存储库 - &gt;网络

0 个答案:

没有答案