Esempio n. 1
0
        public virtual async Task <ActionResult> Join(int?page, int AID)
        {
            InOneDcsnViewModel onedcsn = new InOneDcsnViewModel(page, AID);

            ViewBag.len        = onedcsn.poststoeqry.Count();
            ViewBag.PageNumber = (page ?? 1);
            ViewBag.PageCount  = onedcsn.Pagedpostslist.PageCount;
            Bar = 0;

            return(View(onedcsn));
        }
Esempio n. 2
0
        public virtual async Task <ActionResult> Post(InOneDcsnViewModel model, IEnumerable <HttpPostedFileBase> uploads, int?ParentId, int?AnswerId, int?CurrentPage)
        {
            if (model.mycurrentpost.content == null)
            {
                //return RedirectToAction("Join", new { AID = model.asmtoDiscuss.AID });
                return(Content("Will show a warning dialoge, haha"));
            }
            //return Content(model.LastPage.ToString());
            ApplicationDbContext addpost = new ApplicationDbContext();

            if (AnswerId != null)
            {
                model.mycurrentpost.ToAsmt = addpost.Answers.Find(AnswerId);
            }
            else
            {
                model.mycurrentpost.ToAsmt = addpost.Answers.Find(model.asmtoDiscuss.AID);
            }
            if (ParentId != null)
            {
                model.mycurrentpost.RlyWhich = addpost.Posts.Find(ParentId);
            }
            model.mycurrentpost.Poster  = addpost.Users.Find(User.Identity.GetUserId());
            model.mycurrentpost.pvoting = new Vote();

            //Files(Attachments) upload:
            model.mycurrentpost.FilePaths = new List <FilePath>();
            foreach (var upload in uploads)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    string ext      = Path.GetExtension(upload.FileName).ToLower();
                    string mimeType = "application/unknown";
                    Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
                    if (regKey != null && regKey.GetValue("Content Type") != null)
                    {
                        mimeType = regKey.GetValue("Content Type").ToString();
                    }

                    var PictureExtensions = new[] { ".png", ".jpg", ".gif", ".jpeg" };
                    var showType          = FileType.Tex;
                    if (PictureExtensions.Contains(ext))
                    {
                        showType = FileType.Ref;
                    }


                    var file = new FilePath
                    {
                        FileName     = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName),
                        OrgnFileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType     = showType,
                        ContentType  = mimeType
                    };

                    model.mycurrentpost.FilePaths.Add(file);

                    try
                    {
                        string path = Path.Combine(Server.MapPath("~/Images"),
                                                   Path.GetFileName(file.FileName));
                        upload.SaveAs(path);
                        ViewBag.Message = "File uploaded successfully";
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "ERROR:" + ex.Message.ToString();
                        return(Content(ViewBag.Message));
                    }
                }
            }



            addpost.Posts.Add(model.mycurrentpost);
            addpost.SaveChanges();

            //return View("Join", model);
            int aId = model.mycurrentpost.ToAsmt.AID;

            if (ParentId != null)
            {
                return(RedirectToAction("Join", new { page = model.CurrentPage, AID = aId }));
            }
            else
            {
                if (model.LastPage == 0)
                {
                    model.LastPage += 1;
                }
                return(RedirectToAction("Join", new { page = model.LastPage, AID = aId }));
            }
        }