Example #1
0
        public ActionResult Create(Knowledge knowledge)
        {
            knowledge.CreateTime = DateTime.Now;
            knowledge.CategoryId = 1;
            if (ModelState.IsValid)
            {
                db.Knowledges.Add(knowledge);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Code", knowledge.CategoryId);
            return(View(knowledge));
        }
        public async Task <ActionResult> Create([Bind(Include = "CategoryId,Title,Context")] Knowledge knowledge)
        {
            String[] fileNameArr = null;
            ViewBag.IsEdit     = bool.Parse(Request["IsEdit"]);
            ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", knowledge.CategoryId);
            var accessories = Request["accessoHidden"];

            if (!string.IsNullOrEmpty(accessories))
            {
                fileNameArr = accessories.Split('|');
            }


            if (ModelState.IsValid)
            {
                ActionResult a = await Task.Run <ActionResult>(() =>
                {
                    try
                    {
                        if (ViewBag.IsEdit)
                        {
                            var know        = db.Knowledges.Find(int.Parse(Request["Id"]));
                            know.CreateTime = DateTime.Now;
                            know.Context    = knowledge.Context;
                            know.Title      = knowledge.Title;
                            know.CategoryId = knowledge.CategoryId;
                            know.User       = db.Users.Where(p => p.Email == HttpContext.User.Identity.Name).Single();
                            db.SaveChanges();
                            return(RedirectToAction("Index", "Home"));
                        }

                        var newKnowledge = new Knowledge
                        {
                            CreateTime = DateTime.Now,
                            Context    = knowledge.Context,
                            Title      = knowledge.Title,
                            CategoryId = knowledge.CategoryId,
                            User       = db.Users.Where(p => p.Email == HttpContext.User.Identity.Name).Single(),
                        };



                        db.Knowledges.Add(newKnowledge);
                        db.SaveChanges();
                        if (fileNameArr != null)
                        {
                            foreach (var fileName in fileNameArr)
                            {
                                var file       = db.Accessories.Where(p => p.FileName == fileName).Single();
                                file.Knowledge = newKnowledge;
                            }
                        }

                        db.SaveChanges();
                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex.Message);

                        return(View(knowledge));
                    }
                }



                                                               );

                return(a);
            }
            //foreach (var item in ModelState.Values)
            //{
            //    foreach (var err in item.Errors)
            //    {
            //        ModelState.AddModelError("", err.ErrorMessage);
            //    }
            //}

            return(View(knowledge));
        }