public ActionResult Create(Category model) { try { using (var context = new ShopContainer()) { var category = new Category(); TryUpdateModel(category, new[] { "Name", "SeoDescription", "SeoKeywords", "SortOrder", "Title" }); context.AddToCategory(category); context.SaveChanges(); return RedirectToAction("Category", "Home", new { area = "", id = category.Name }); } } catch { return View(); } }
public ActionResult Create(int? parentId, FormCollection form, HttpPostedFileBase uploadFile) { try { using (var context = new ShopContainer()) { var category = new Category(); TryUpdateModel(category, new[] { "Name", "SeoDescription", "SeoKeywords", "SortOrder","Title" }); if (uploadFile != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", uploadFile.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); uploadFile.SaveAs(filePath); category.ImageSource = fileName; } if (parentId.HasValue) { var parent = context.Category.First(c => c.Id == parentId); parent.Children.Add(category); } else { context.AddToCategory(category); } context.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); } }