public ActionResult DeleteConfirmed(long id, string returnQueryString) { Document data = null; try { using (MyDocumentContext context = new MyDocumentContext()) { data = context.Documents.Find(id); if (data == null) { return(HttpNotFound()); } context.Documents.Remove(data); context.SaveChanges(); } return(RedirectToAction( "Index", ReturnQueryRouteValueBuilder.GetReturnQueryRouteValue(returnQueryString))); } catch (Exception ex) { logger.Error(ex.Message, ex); ViewBag.Message = "服务器忙.请稍候在尝试执行本操作。"; return(View(data)); } }
// // GET: /Document/ public ActionResult Index( int?pageNo, int?pageSize) { List <Document> dataList = null; using (MyDocumentContext context = new MyDocumentContext()) { var query = from data in context.Documents select data; // 初始化翻页. PageInfo pgInfo = new PageInfo( pageSize: pageSize, pageNo: pageNo, rowCount: query.Count()); ViewBag.PageInfo = pgInfo; query = query.OrderBy(p => p.DocumentID) .Skip(pgInfo.SkipValue) .Take(pgInfo.PageSize); // 查询数据. dataList = query.ToList(); } // 显示画面. return(View(dataList)); }
/// <summary> /// 取得文档列表. /// </summary> /// <param name="typeCode"></param> /// <param name="pgInfo"></param> /// <returns></returns> public List <Document> GetDocumentList(string typeCode, ref PageInfo pgInfo) { using (MyDocumentContext context = new MyDocumentContext()) { var query = from data in context.Documents where data.DocumentTypeCode == typeCode select data; pgInfo = new PageInfo( pageSize: pgInfo.PageSize, pageNo: pgInfo.PageIndex, rowCount: query.Count()); query = query.OrderByDescending(p => p.CreateTime) .Skip(pgInfo.SkipValue) .Take(pgInfo.PageSize); List <Document> resultList = query.ToList(); return(resultList); } }
// // GET: /DocumentType/ public ActionResult Index( string searchDocumentTypeCode, string searchDocumentTypeName, int?pageNo, int?pageSize) { List <DocumentType> dataList = null; using (MyDocumentContext context = new MyDocumentContext()) { var query = from data in context.DocumentTypes select data; if (!String.IsNullOrEmpty(searchDocumentTypeCode)) { query = query.Where(p => p.DocumentTypeCode.Contains(searchDocumentTypeCode)); } if (!String.IsNullOrEmpty(searchDocumentTypeName)) { query = query.Where(p => p.DocumentTypeName.Contains(searchDocumentTypeName)); } // 初始化翻页. PageInfo pgInfo = new PageInfo( pageSize: pageSize, pageNo: pageNo, rowCount: query.Count()); ViewBag.PageInfo = pgInfo; query = query.OrderBy(p => p.DocumentTypeCode) .Skip(pgInfo.SkipValue) .Take(pgInfo.PageSize); // 查询数据. dataList = query.ToList(); } // 显示画面. return(View(dataList)); }
// // GET: /Document/Details/5 public ActionResult Details(long id, string returnQueryString) { Document data = null; using (MyDocumentContext context = new MyDocumentContext()) { data = context.Documents.Find(id); } if (data == null) { return(HttpNotFound()); } // 设置返回的 RouteValueDictionary ViewBag.ReturnQueryRouteValue = ReturnQueryRouteValueBuilder.GetReturnQueryRouteValue(returnQueryString); return(View(data)); }
static void Main(string[] args) { // 当 Code First 与数据库结构不一致时 // 自动升级到最新的版本. Database.SetInitializer(new MigrateDatabaseToLatestVersion <MyDocumentContext, MyDocument.Migrations.Configuration>()); using (MyDocumentContext context = new MyDocumentContext()) { var query = from data in context.DocumentTypes select data; foreach (var item in query) { Console.WriteLine(item.DocumentTypeName); } } Console.WriteLine("Finish!"); Console.ReadLine(); }
public ActionResult Edit(Document data, string returnQueryString, HttpPostedFileBase pdfFile, HttpPostedFileBase logoFile) { if (pdfFile != null && !String.IsNullOrEmpty(pdfFile.FileName)) { // 上传了文件. // 取得上传的文件名. string uploadFileName = System.IO.Path.GetFileName(pdfFile.FileName); if (!UploadFileChecker.CheckPdfFileExt(uploadFileName)) { ViewBag.Message = "文件仅仅允许上传 PDF 扩展名的文件!"; return(View(data)); } data.DocumentFileName = SavePdfFile(pdfFile); data.DocumentSwfFileName = SaveSwfFile(data.DocumentFileName); } if (logoFile != null && !String.IsNullOrEmpty(logoFile.FileName)) { // 上传了文件. // 取得上传的文件名. string uploadFileName = System.IO.Path.GetFileName(logoFile.FileName); if (!UploadFileChecker.CheckImageFileExt(uploadFileName)) { ViewBag.Message = String.Format("文件仅仅允许上传 {0} 扩展名的文件!", UploadFileChecker.DisplayAccessAbleImageExt); return(View(data)); } data.DocumentLogo = SaveLogoFile(logoFile); } try { if (ModelState.IsValid) { // 假如数据检查无误. // 更新数据. using (MyDocumentContext context = new MyDocumentContext()) { Document oldDocument = context.Documents.Find(data.DocumentID); if (oldDocument == null) { ViewBag.Message = "数据不存在!"; return(View(data)); } DataRecorder.BeforeUpdateOperation(Session, data); // 先从上下文中的旧实体获取跟踪 DbEntityEntry entry = context.Entry(oldDocument); // 把新值设置到旧实体上 entry.CurrentValues.SetValues(data); // 物理保存. context.SaveChanges(); } return(RedirectToAction( "Index", ReturnQueryRouteValueBuilder.GetReturnQueryRouteValue(returnQueryString))); } else { // 假如数据检查有误, 那么返回原有的画面,让用户继续修改. return(View(data)); } } catch (Exception ex) { logger.Error(ex.Message, ex); ViewBag.Message = "服务器忙.请稍候在尝试执行本操作。"; return(View(data)); } }
public ActionResult Create(DocumentType data, string returnQueryString, HttpPostedFileBase logoFile) { if (logoFile != null && !String.IsNullOrEmpty(logoFile.FileName)) { // 上传了文件. // 取得上传的文件名. string uploadFileName = System.IO.Path.GetFileName(logoFile.FileName); if (!UploadFileChecker.CheckImageFileExt(uploadFileName)) { ViewBag.Message = String.Format("文件仅仅允许上传 {0} 扩展名的文件!", UploadFileChecker.DisplayAccessAbleImageExt); return(View(data)); } data.DocumentTypeLogo = SaveLogoFile(logoFile); } try { if (ModelState.IsValid) { // 假如数据检查无误. // 将数据加入结果列表. using (MyDocumentContext context = new MyDocumentContext()) { DocumentType oldDocumentType = context.DocumentTypes.Find(data.DocumentTypeCode); if (oldDocumentType != null) { ViewBag.Message = "代码已存在!"; return(View()); } DataRecorder.BeforeInsertOperation(Session, data); context.DocumentTypes.Add(data); context.SaveChanges(); } return(RedirectToAction( "Index", ReturnQueryRouteValueBuilder.GetReturnQueryRouteValue(returnQueryString))); } else { // 假如数据检查有误, 那么返回原有的画面,让用户继续修改. return(View(data)); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbErr) { foreach (var errItem in dbErr.EntityValidationErrors) { foreach (var err in errItem.ValidationErrors) { logger.InfoFormat("{0} : {1}", err.PropertyName, err.ErrorMessage); } } logger.Error(dbErr.Message, dbErr); ViewBag.Message = dbErr.Message; return(View(data)); } catch (Exception ex) { logger.Error(ex.Message, ex); ViewBag.Message = "服务器忙.请稍候在尝试执行本操作。"; return(View(data)); } }