public ActionResult DeleteConfirmed(int id) { OwnerAsset ownerAsset = db.OwnerAssets.Find(id); db.OwnerAssets.Remove(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "OwnerAssetID,AssetName,OwnerID,AssetPhoto,SpecialNotes,IsActive,DateAdded")] OwnerAsset ownerAsset, HttpPostedFileBase AssetPhoto) { if (ModelState.IsValid) { ownerAsset.OwnerID = User.Identity.GetUserId(); //*********** USER INFO AND FILE IMAGE UPLOAD #region User Information and File/Image Upload //default image will be noImage.jpg if no image is provided string image = "noImage.jpg"; //check that image upload contains valid image if (AssetPhoto != null) { //yes //reassign the fileName to the variable that represents the default img image = AssetPhoto.FileName; //create a variable and retrieve the extension from the image string ext = image.Substring(image.LastIndexOf(".")); //create a list of valid file extensions - (whitelist) string[] goodExts = new string[] { ".jpg", ".png", ".jpeg", ".gif" }; //check our extension against that list if (goodExts.Contains(ext.ToLower())) { //as long as our extension is in that list //rename the file to a unique file name and add the extension image = Guid.NewGuid() + ext; //save the new file to the website AssetPhoto.SaveAs(Server.MapPath("~/Content/assets/images/UserImages/" + image)); } //if an invalid extension is provided else { //go back to the default page image = "noImage.jpg"; } } //No Matter What add the image name to the database object ownerAsset.AssetPhoto = image; #endregion ownerAsset.DateAdded = DateTime.Now; ownerAsset.IsActive = true; db.OwnerAssets.Add(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(ownerAsset)); }
public ActionResult DeleteConfirmed(int id) { OwnerAsset ownerAsset = db.OwnerAssets.Find(id); if (ownerAsset.AssetPhoto != null && ownerAsset.AssetPhoto != "noImage.png") { System.IO.File.Delete(Server.MapPath("~/Content/dogImages/" + ownerAsset.AssetPhoto)); } db.OwnerAssets.Remove(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "OwnerAssetID,AssetName,OwnerID,AssetPhoto,SpecialNotes,IsActive,DateAdded")] OwnerAsset ownerAsset, HttpPostedFileBase assetImage) { if (ModelState.IsValid) { //default image string imageName = "noImage.png"; //if file was sent if (assetImage != null) { imageName = assetImage.FileName; string ext = imageName.Substring(imageName.LastIndexOf('.')); string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" }; //if extension is valid if (goodExts.Contains(ext.ToLower())) { imageName = Guid.NewGuid() + ext; assetImage.SaveAs(Server. MapPath("~/Content/images/Photos/" + imageName)); } else { imageName = "noImage.png"; } } ownerAsset.AssetPhoto = imageName; //Dynamically grabs current logged in user and sets it as the OwnerID if the user is a client if (User.IsInRole("Client")) { string currentUser = User.Identity.GetUserId(); ownerAsset.OwnerID = currentUser; } //Automatically sets the IsActive property to true ownerAsset.IsActive = true; //Dynamically adds the current date ownerAsset.DateAdded = DateTime.Now; db.OwnerAssets.Add(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnerID = new SelectList(db.UserDetails, "UserID", "FirstName", ownerAsset.OwnerID); return(View(ownerAsset)); }
public ActionResult DeleteConfirmed(int id) { OwnerAsset ownerAsset = db.OwnerAssets.Find(id); //if (ownerAsset.AssetPhoto != null && ownerAsset.AssetPhoto != "NoImage.png") //{ // System.IO.File.Delete(Server.MapPath("~/Content/Images/" + Session["currentImage"].ToString())); //} db.OwnerAssets.Remove(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "OwnerAssetID,AssetName,OwnerID,AssetPhoto,SpecialNotes,IsActive,DateAdded")] OwnerAsset ownerAsset, HttpPostedFileBase AssetPhoto) { if (ModelState.IsValid) { ownerAsset.OwnerID = User.Identity.GetUserId(); //******************** FILE IMAGE UPLOAD #region User Information and File/Image Upload //if image is valid if (AssetPhoto != null) { //store the new file name string image = AssetPhoto.FileName; //extract the extension and save it in a variable string ext = image.Substring(image.LastIndexOf(".")); //valid file extensions - (whitelist) string[] goodExts = new string[] { ".jpg", ".png", ".jpeg", ".gif" }; //check our extension against that list if (goodExts.Contains(ext.ToLower())) { //if it's good...guid it. //rename the file to a unique file name and add the extension image = Guid.NewGuid() + ext; //save the new file to the website AssetPhoto.SaveAs(Server.MapPath("~/Content/assets/images/UserImages/" + image)); //remove the original(previous) image from website (NOT default image) if (ownerAsset.AssetPhoto != "noImage.jpg") { System.IO.File.Delete(Server.MapPath("~/Content/assets/images/UserImages/" + ownerAsset.AssetPhoto)); } //save to database ownerAsset.AssetPhoto = image; } } //if upload is null the HiddenFor() will save original file #endregion db.Entry(ownerAsset).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(ownerAsset)); }
// GET: OwnerAssets/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OwnerAsset ownerAsset = db.OwnerAssets.Find(id); if (ownerAsset == null) { return(HttpNotFound()); } return(View(ownerAsset)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OwnerAsset ownerAsset = db.OwnerAssets.Find(id); if (ownerAsset == null) { return(HttpNotFound()); } ViewBag.OwnerId = new SelectList(db.UserDetails, "UserId", "FirstName", ownerAsset.OwnerId); return(View(ownerAsset)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OwnerAsset ownerAsset = db.OwnerAssets.Find(id); if (ownerAsset == null) { return(HttpNotFound()); } ViewBag.UserID = new SelectList(db.OwnerInformations, "UserID", "FullName", ownerAsset.UserID); return(View(ownerAsset)); }
public ActionResult DeleteConfirmed(int id) { OwnerAsset ownerAsset = db.OwnerAssets.Find(id); db.OwnerAssets.Remove(ownerAsset); //remove associated image(keeping default) if (ownerAsset.AssetPhoto != "noImage.jpg" && ownerAsset.AssetPhoto != null) { System.IO.File.SetAttributes(Server.MapPath("~/Content/assets/images/UserImages/" + ownerAsset.AssetPhoto), FileAttributes.Normal); System.IO.File.Delete(Server.MapPath("~/Content/assets/images/UserImages/" + ownerAsset.AssetPhoto)); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "OwnerAssetId,AssetName,OwnerId,AssetPhoto,SpecialNotes,IsActive,DateAdded,Relationship")] OwnerAsset ownerAsset, HttpPostedFileBase image) { if (ModelState.IsValid) { #region File Upload string imageName = "noImage.png"; if (image != null) { //get the file name for image imageName = image.FileName; string ext = imageName.Substring(imageName.LastIndexOf('.')); //create a list of valid extensions string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" }; if (goodExts.Contains(ext.ToLower()) && image.ContentLength <= 4194304) { imageName = Guid.NewGuid() + ext; image.SaveAs(Server.MapPath("~/Content/images/" + imageName)); } else { imageName = "noImage.png"; } } ownerAsset.AssetPhoto = imageName; #endregion ownerAsset.DateAdded = @DateTime.Now;//added for it automatically update the time if (!User.IsInRole("Admin")) { ownerAsset.OwnerId = User.Identity.GetUserId();//add this to add assets to their userid } db.OwnerAssets.Add(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnerId = new SelectList(db.UserDetails, "UserId", "FullName", ownerAsset.OwnerId); return(View(ownerAsset)); }
public ActionResult Create([Bind(Include = "OwnerAssetID,AssetRegisteredName,AssetCallName,AssetSpecies,AssetBreed,AssetAge,AssetSize,AssetTrainerCertified,UserID,AssetPhoto,SpecialNotes,IsActive,DateAdded,DescriptiveColorProfile")] OwnerAsset ownerAsset, HttpPostedFileBase myImg) { if (ModelState.IsValid) { #region PhotoUpload string imageName = "noImage.png"; if (myImg != null) { //Get File Extension - alternative to using a Substring () and Indexof() string imgExt = System.IO.Path.GetExtension(myImg.FileName); //list of allowed extentions string[] allowedExtentions = { ".png", ".gif", ".jpg", ".jpeg" }; if (allowedExtentions.Contains(imgExt.ToLower()) && (myImg.ContentLength <= 4194304)) { //using GUid for saved file name imageName = Guid.NewGuid() + imgExt; //creat some variables to pass to the resize image method //Signature to resize Image string savePath = Server.MapPath("~/Content/assets/Image/"); Image convertedImage = Image.FromStream(myImg.InputStream); int maxImgSize = 1200; int maxThumbSize = 100; //calling this method will use the variables created above will save the full size image and thumbnail img to your server. UploadUtility.ResizeImage(savePath, imageName, convertedImage, maxImgSize, maxThumbSize); ViewBag.PhotoMessage = "Photo Upload Complete."; } else { imageName = "noImage.png"; } ownerAsset.AssetPhoto = imageName; } #endregion db.OwnerAssets.Add(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.UserID = new SelectList(db.OwnerInformations, "UserID", "FullName", ownerAsset.UserID); return(View(ownerAsset)); }
public ActionResult Edit([Bind(Include = "OwnerAssetID,AssetName,UserID,AssetPhoto,SpecialNotes,IsActive,DateAdded")] OwnerAsset ownerAsset, HttpPostedFileBase fupImage) { if (ownerAsset.UserID == null) { ownerAsset.UserID = User.Identity.GetUserId(); } if (ModelState.IsValid) { //File upload for Edit if (fupImage != null) { string imgName = fupImage.FileName; imgName = fupImage.FileName; string ext = imgName.Substring(imgName.LastIndexOf('.')); string[] goodExts = { ".jpeg", ".jpg", ".gif", ".png" }; if (goodExts.Contains(ext.ToLower()) && (fupImage.ContentLength <= 4194304)) { fupImage.SaveAs(Server.MapPath("~/Content/dogImages/" + imgName)); if (ownerAsset.AssetPhoto != null && ownerAsset.AssetPhoto != "noImage.png") { System.IO.File.Delete(Server.MapPath("~/Content/dogImages/" + ownerAsset.AssetPhoto)); } ownerAsset.AssetPhoto = imgName; } else { imgName = "noImage.png"; throw new ApplicationException("Incorrect file type. 1) Please use one of the following: (png, jpg, jpeg, or gif). " + " 2) File size may exceed the 4MB limit. Please reduce the file size and try again."); } } db.Entry(ownerAsset).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(ownerAsset)); }
public ActionResult Edit([Bind(Include = "OwnerAssetID,AssetName,OwnerID,AssetPhoto,SpecialNotes,IsActive,DateAdded")] OwnerAsset ownerAsset, HttpPostedFileBase assetImage) { if (ModelState.IsValid) { if (assetImage != null) { string imageName = assetImage.FileName; string ext = imageName.Substring(imageName.LastIndexOf('.')); string[] goodExts = { ".jpg", ".jpeg", ".png", ".gif" }; if (goodExts.Contains(ext.ToLower())) { imageName = Guid.NewGuid() + ext; assetImage.SaveAs(Server.MapPath("~/Content/images/Photos/" + imageName)); ownerAsset.AssetPhoto = imageName; } } //Dynamically grabs current logged in user and sets it as the OwnerID if the user is not an Admin if (User.IsInRole("Client")) { string currentUser = User.Identity.GetUserId(); ownerAsset.OwnerID = currentUser; } if (!User.IsInRole("Admin")) { //Automatically sets the IsActive property to true ownerAsset.IsActive = true; } db.Entry(ownerAsset).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnerID = new SelectList(db.UserDetails, "UserID", "FirstName", ownerAsset.OwnerID); return(View(ownerAsset)); }
public ActionResult Create([Bind(Include = "OwnerAssetId,ChildName,OwnerId,ChildPhoto,SpecialNotes,IsActive,DateAdded,ChildDOB,SkillLevel")] OwnerAsset ownerAsset, HttpPostedFileBase childImage) { if (ModelState.IsValid) { #region FileUpload string imageName = "noImage.png"; if (childImage != null) { imageName = childImage.FileName; string ext = imageName.Substring(imageName.LastIndexOf('.')); string[] goodExts = new string[] { ".jpeg", ".jpg", ".png", ".gif" }; if (goodExts.Contains(ext.ToLower())) { imageName = Guid.NewGuid() + ext; childImage.SaveAs(Server.MapPath("~/Content/images/" + imageName)); } else { imageName = "noImage.png"; } } ownerAsset.ChildPhoto = imageName; if (User.IsInRole("User")) { ownerAsset.OwnerId = User.Identity.GetUserId(); } #endregion ownerAsset.IsActive = true; ownerAsset.DateAdded = DateTime.Now; db.OwnerAssets.Add(ownerAsset); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnerId = new SelectList(db.UserDetails, "UserId", "Parent", ownerAsset.OwnerId); return(View(ownerAsset)); }