public ActionResult CreateBlog(BlogsModel _Blogsmodel, string command, FormCollection fm) { Session["Edit/Delete"] = "Edit"; var file = Request.Files.Count > 0 ? Request.Files[0] : null; ViewBag.Title = (_Blogsmodel.BlogID > 0 ? "Edit " : "Add ") + " Post Details "; ViewBag.Submit = _Blogsmodel.BlogID > 0 ? "Update" : "Save"; bool isActive = true, allowComments = true; _Blogsmodel.TagList = GetAllTags(); var _UsersContexts = new Contexts.UsersContexts(); var _CategoriesContexts = new Contexts.CategoriesContexts(); var BlogsContext = new Contexts.BlogsContexts(); var TagContext = new Contexts.TagsContexts(); ViewBag.AuthorName = new SelectList(_UsersContexts.GetUserList(_Blogsmodel.AuthorNameID), "UserID", "UserNameTxt");//Author drop down listing ViewBag.Category = new SelectList(_CategoriesContexts.GetSelectedCategories(string.Empty), "CategoryID", "CategoryNameTxt"); if (string.IsNullOrEmpty(command)) { isActive = _Blogsmodel.IsActiveInd; allowComments = _Blogsmodel.IsCommentEnabledInd; ViewBag.IsCommentEnabledInd = Models.Common.GetStatusListBoolean(allowComments ? "true" : "false"); ViewBag.IsActiveInd = Models.Common.GetStatusListBoolean(isActive ? "true" : "false"); _Blogsmodel.SocialMediaList = GetAllSocialMedia(); _Blogsmodel.strCategoryid = Convert.ToString(_Blogsmodel.strCategoryid); if (BlogsContext.GetBlogs().Where(x => x.TitleTxt == _Blogsmodel.TitleTxt && _Blogsmodel.BlogID != x.BlogID).Any()) { ModelState.AddModelError("TitleTxt", _Blogsmodel.TitleTxt + " Post already exists."); return(View(_Blogsmodel)); } if (_Blogsmodel.SlagTxt.ToLower() == "error404") //check 404 error { ModelState.AddModelError("SlagTxt", _Blogsmodel.SlagTxt + " URL is not allowed."); return(View(_Blogsmodel)); } try { //Save image path if (file != null && file.ContentLength > 0) { #region Upload Image Models.Common.CreateFolder(); var croppedfile = new System.IO.FileInfo(Server.MapPath(TempData["CroppedImage"].ToString())); var fileName = croppedfile.Name; croppedfile = null; var sourcePath = Server.MapPath(TempData["CroppedImage"].ToString()); var targetPath = Request.PhysicalApplicationPath + "WebData\\"; System.IO.File.Copy(System.IO.Path.Combine(sourcePath.Replace(fileName, ""), fileName), System.IO.Path.Combine(targetPath + "images\\", fileName), true); try { Models.Common.DeleteImage(Server.MapPath(TempData["CroppedImage"].ToString())); } catch { } TempData["CroppedImage"] = null; _Blogsmodel.ImagePathTxt = "~/WebData/images/" + fileName; var width = 250; var fileExtension = fileName.Substring(fileName.LastIndexOf("."), fileName.Length - fileName.LastIndexOf(".")); var strPath = Request.PhysicalApplicationPath + "WebData\\images\\" + fileName; var myImage = Models.Common.CreateImageThumbnail(strPath, width); myImage.Save(Request.PhysicalApplicationPath + "WebData\\thumbnails\\" + fileName, fileExtension.ToLower() == ".png" ? System.Drawing.Imaging.ImageFormat.Png : fileExtension.ToLower() == ".gif" ? System.Drawing.Imaging.ImageFormat.Gif : System.Drawing.Imaging.ImageFormat.Jpeg ); myImage.Dispose(); var mysmallImage = Models.Common.CreateImageThumbnail(strPath, 200); mysmallImage.Save(Request.PhysicalApplicationPath + "WebData\\thumbnails_Small\\" + fileName, fileExtension.ToLower() == ".png" ? System.Drawing.Imaging.ImageFormat.Png : fileExtension.ToLower() == ".gif" ? System.Drawing.Imaging.ImageFormat.Gif : System.Drawing.Imaging.ImageFormat.Jpeg ); mysmallImage.Dispose(); #endregion } else { _Blogsmodel.ImagePathTxt = BlogsContext.GetBlogs().Where(x => x.BlogID == _Blogsmodel.BlogID).Select(x => x.ImagePathTxt).FirstOrDefault(); } if (ViewBag.Submit == "Save") { BlogsContext.AddBlogs(_Blogsmodel); TempData["AlertMessage"] = "Post details saved successfully."; } else { BlogsContext.EditBlogs(_Blogsmodel, false); TempData["AlertMessage"] = "Post details updated successfully."; } } catch (Exception ex) { TempData["AlertMessage"] = "Some error occured, Please try after some time. " + ex.Message; } } var rvd = new RouteValueDictionary(); rvd.Add("Column", Request.QueryString["Column"] != null ? Request.QueryString["Column"].ToString() : "PostedDate"); rvd.Add("Direction", Request.QueryString["Direction"] != null ? Request.QueryString["Direction"].ToString() : "Descending"); rvd.Add("pagesize", Request.QueryString["pagesize"] != null ? Request.QueryString["pagesize"].ToString() : Models.Common._pageSize.ToString()); rvd.Add("page", Request.QueryString["page"] != null ? Request.QueryString["page"].ToString() : Models.Common._currentPage.ToString()); return(RedirectToAction("BlogsListing", "Blogs", rvd)); }
public ActionResult CreateBlog(int?Blogid) { var _BlogsContexts = new Contexts.BlogsContexts(); var _CategoriesContexts = new Contexts.CategoriesContexts(); var _UsersContexts = new Contexts.UsersContexts(); var _BlogsModel = new BlogsModel(); ViewBag.Title = (Blogid.HasValue ? "Edit " : "Add ") + " Post Details "; ViewBag.Submit = Blogid.HasValue && Blogid.Value > 0 ? "Update" : "Save"; bool isActive = true, allowComments = true; _BlogsModel.TagList = GetAllTags(); _BlogsModel.SocialMediaList = GetAllSocialMedia(); try { var taglist = ViewBag.Tag = string.Join(",", _BlogsModel.TagList.Select(x => x.Text)); if (Blogid.HasValue && Blogid.Value > 0) { _BlogsModel = _BlogsContexts.GetBlogs().Where(x => x.BlogID == Blogid).FirstOrDefault(); if (_BlogsModel != null) { isActive = _BlogsModel.IsActiveInd; allowComments = _BlogsModel.IsCommentEnabledInd; _BlogsModel.strCategoryid = Convert.ToString(_BlogsModel.CategoryID); var selecetdtagNamearray = Array.ConvertAll <string, string>(_BlogsContexts.GetFormBlogTags(Blogid.Value).ToArray(), delegate(string i) { return((string)i.ToString()); }); var selecetdtagIDarray = Array.ConvertAll <string, string>(_BlogsContexts.GetFormBlogTagsIDList(Blogid.Value).ToArray(), delegate(string i) { return((string)i.ToString()); }); var selecetdSocialMediasarray = Array.ConvertAll <int, string>(_BlogsContexts.GetFormBlogSocialMedia(Blogid.Value).ToArray(), delegate(int i) { return((string)i.ToString()); }); var UserList = _UsersContexts.GetUser(_BlogsModel.AuthorNameTxt); //User list based on userid _BlogsModel.AuthorNameID = Convert.ToString(UserList != null ? UserList.UserID : 0); //UserID ViewBag.Category = new SelectList(_CategoriesContexts.GetSelectedCategories(Convert.ToString(_BlogsModel.CategoryID)), "CategoryID", "CategoryNameTxt"); ViewBag.AuthorName = new SelectList(_UsersContexts.GetUserList(_BlogsModel.AuthorNameID), "UserID", "UserNameTxt"); //Author drop down listing _BlogsModel.TagList = GetAllTags(); _BlogsModel.SocialMediaList = GetAllSocialMedia(); _BlogsModel.SelectedTags = selecetdtagNamearray; _BlogsModel.SelectedTagsID = selecetdtagIDarray; _BlogsModel.SelectedSocialMedia = selecetdSocialMediasarray; } } else { ViewBag.Category = new SelectList(_CategoriesContexts.GetSelectedCategories(string.Empty), "CategoryID", "CategoryNameTxt"); ViewBag.AuthorName = new SelectList(_UsersContexts.GetUserList(string.Empty), "UserID", "UserNameTxt");//Author drop down listing string[] Tagsarr = new string[] { "0" }; _BlogsModel.SelectedTagsID = Tagsarr; } } catch (Exception ex) { ViewBag.Category = new SelectList(_CategoriesContexts.GetSelectedCategories(string.Empty), "CategoryID", "CategoryNameTxt"); ViewBag.AuthorName = new SelectList(_UsersContexts.GetUserList(string.Empty), "UserID", "UserNameTxt");//Author drop down listing string[] Tagsarr = new string[] { "0" }; _BlogsModel.SelectedTagsID = Tagsarr; } ViewBag.IsCommentEnabledInd = Models.Common.GetStatusListBoolean(allowComments ? "true" : "false"); ViewBag.IsActiveInd = Models.Common.GetStatusListBoolean(isActive ? "true" : "false"); return(View(_BlogsModel)); }