public ActionResult Create(StudentAdvisingRemark studentadvisingremark, string opener_id = null)
        {
            if (ModelState.IsValid)
            {
                db.StudentAdvisingRemarks.Add(studentadvisingremark);
                try
                {
                    studentadvisingremark.created = DateTime.Now;
                    studentadvisingremark.created_by = User.Identity.Name;
                    studentadvisingremark.modified = DateTime.Now;
                    studentadvisingremark.modified_by = User.Identity.Name;
                    db.SaveChanges();
                    if (!String.IsNullOrEmpty(studentadvisingremark.filename) && !String.IsNullOrEmpty(studentadvisingremark.filepath))
                    {
                        var sourcePath = Server.MapPath("~/App_Data/" + studentadvisingremark.filepath);
                        var sourceFilepath = Path.Combine(sourcePath, studentadvisingremark.filename);
                        var destPath = Server.MapPath("~/App_Data/" + "Attachments/AdvisingRemark/" + studentadvisingremark.id);
                        var destFilepath = Path.Combine(destPath, studentadvisingremark.filename);
                        try
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to create directory." + e.Message;
                        }
                        try
                        {
                            System.IO.File.Move(sourceFilepath, destFilepath);
                            studentadvisingremark.filepath = "Attachments/AdvisingRemark/" + studentadvisingremark.id;
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to move file." + e.Message;
                        }
                    }

                    //clear temp files uploaded but not used
                    if (Directory.Exists(Server.MapPath("~/App_Data/Temp/AdvisingRemark/" + User.Identity.Name)))
                    {
                        var files = Directory.GetFiles(Server.MapPath("~/App_Data/Temp/AdvisingRemark/" + User.Identity.Name));
                        foreach (var file in files)
                        {
                            System.IO.File.Delete(file);
                        }
                    }
                }
                catch (Exception e)
                {
                    return HttpNotFound("Failed to add remark.<br/><br/>" + e.Message);
                }
            }
            return RedirectToAction("AdvisingRemark", "StudentProfile", new { student_id = studentadvisingremark.student_id, opener_id = opener_id });
        }
 public ActionResult Create(string student_id = null, string opener_id = null)
 {
     ViewBag.opener_id = opener_id;
     var student = db.StudentProfiles.Find(student_id);
     if (student == null)
     {
         return HttpNotFound("Student Profile not found.");
     }
     StudentAdvisingRemark studentadvisingremark = new StudentAdvisingRemark
     {
         display_date = DateTime.Now,
         @private = true,
         student_id = student.id,
         StudentProfile = student
     };
     return View(studentadvisingremark);
 }
        public ActionResult Edit(StudentAdvisingRemark studentadvisingremark, string opener_id = null)
        {
            if (ModelState.IsValid)
            {
                var remark = db.StudentAdvisingRemarks.Find(studentadvisingremark.id);
                if (remark.filename != studentadvisingremark.filename || remark.filepath != studentadvisingremark.filepath) // action only when existing filename is diff from posted one
                {
                    if (!String.IsNullOrEmpty(remark.filename)) //delete existing file if filename id not empty
                    {
                        var path = Server.MapPath("~/App_Data/" + remark.filepath);
                        var filepath = Path.Combine(path, remark.filename);
                        if (System.IO.File.Exists(filepath))
                        {
                            try
                            {
                                System.IO.File.Delete(filepath);
                            }
                            catch (Exception e)
                            {
                                Session["FlashMessage"] = "Failed to delete attachment." + e.Message;
                            }
                        }
                    }
                    if (!String.IsNullOrEmpty(studentadvisingremark.filename) && !String.IsNullOrEmpty(studentadvisingremark.filepath)) // move the uploaded file to destination
                    {
                        var sourcePath = Server.MapPath("~/App_Data/" + studentadvisingremark.filepath);
                        var sourceFilepath = Path.Combine(sourcePath, studentadvisingremark.filename);
                        var destPath = Server.MapPath("~/App_Data/" + "Attachments/AdvisingRemark/" + studentadvisingremark.id);
                        var destFilepath = Path.Combine(destPath, studentadvisingremark.filename);
                        try
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to create directory." + e.Message;
                        }
                        try
                        {
                            System.IO.File.Move(sourceFilepath, destFilepath);
                            studentadvisingremark.filepath = "Attachments/AdvisingRemark/" + studentadvisingremark.id;
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to move file." + e.Message;
                        }
                    }

                    //clear temp files uploaded but not used
                    if (Directory.Exists(Server.MapPath("~/App_Data/Temp/AdvisingRemark/" + studentadvisingremark.id)))
                    {
                        var files = Directory.GetFiles(Server.MapPath("~/App_Data/Temp/AdvisingRemark/" + studentadvisingremark.id));
                        foreach (var file in files)
                        {
                            System.IO.File.Delete(file);
                        }
                    }
                }
                studentadvisingremark.modified = DateTime.Now;
                studentadvisingremark.modified_by = User.Identity.Name;
                db.Entry(remark).CurrentValues.SetValues(studentadvisingremark);
                db.SaveChanges();
            }
            return RedirectToAction("AdvisingRemark", "StudentProfile", new { student_id = studentadvisingremark.student_id, opener_id = opener_id });
        }