public ActionResult Create(Post model, HttpPostedFileBase uploadImage) { if (ModelState.IsValid) { if (uploadImage != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(uploadImage.InputStream)) { imageData = binaryReader.ReadBytes(uploadImage.ContentLength); } model.Image = imageData; } else { model.Image = null; } Post post = null; DateTime currentDate = DateTime.Now; using (PostContext db = new PostContext()) { post = db.Posts.Where(p => p.Name == model.Name).FirstOrDefault(); } if (post == null) { using (PostContext db = new PostContext()) { Post curPost = new Post { Name = model.Name, Content = model.Content, Price = model.Price, CreationDate = currentDate, UserName = User.Identity.Name, Image = model.Image }; db.Posts.Add(curPost); db.SaveChanges(); LuceneSearch.AddUpdateLuceneIndex(curPost); post = db.Posts.Where(p => p.Name == model.Name).FirstOrDefault(); } if (post != null) { return(RedirectToAction("Index", "Posts")); } } else { ModelState.AddModelError("", "Объявление с таким названием уже существует!"); } } return(View(model)); }
// GET: Posts public ActionResult Index(string searchStr) { List <Post> posts = null; using (PostContext db = new PostContext()) { //LuceneSearch.AddUpdateLuceneIndex(db.Posts.ToList()); if (searchStr != null) { posts = LuceneSearch.Search(searchStr); } else { posts = db.Posts.ToList(); } } return(View(posts)); }