Ejemplo n.º 1
0
        public ActionResult Create(FormCollection form)
        {
            try
            {
                using (var context = new CatalogueContainer())
                {
                    var category = new Category();


                    var attributes = context.CategoryAttribute.ToList();
                    PostCheckboxesData postData = form.ProcessPostCheckboxesData("attr");
                    foreach (var kvp in postData)
                    {
                        var attribute = attributes.First(a => a.Id == kvp.Key);
                        if (kvp.Value)
                        {
                            if (!category.CategoryAttributes.Contains(attribute))
                                category.CategoryAttributes.Add(attribute);
                        }
                        else
                        {
                            if (category.CategoryAttributes.Contains(attribute))
                                category.CategoryAttributes.Remove(attribute);
                        }
                    }

                    TryUpdateModel(category, new[] { 
                    "Name", 
                    "Title", 
                    "SortOrder",
                    "SeoDescription",
                    "SeoKeywords",
                    "DescriptionTitle"
                    });
                    category.Description = HttpUtility.HtmlDecode(form["Description"]);
                    category.Name = category.Name.ToLower().Replace(" ", "");

                    context.AddToCategory(category);
                    context.SaveChanges();
                    return RedirectToAction("Index", "Category", new { area = "Admin" });
                }


            }
            catch
            {
                return View();
            }
        }