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));
            }
        }
Пример #2
0
        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));
            }
        }