Esempio n. 1
0
 public ActionResult Create()
 {
     using (var context = new SiteContainer())
     {
         var category = new Category { SortOrder = 0 };
         var attributes = context.ProductAttribute.ToList();
         ViewBag.Attributes = attributes;
         return View(category);
     }
 }
Esempio n. 2
0
        public ActionResult Create(FormCollection form)
        {
            using (var context = new SiteContainer())
            {
                var category = new Category();


                var attributes = context.ProductAttribute.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.ProductAttributes.Contains(attribute))
                            category.ProductAttributes.Add(attribute);
                    }
                    else
                    {
                        if (category.ProductAttributes.Contains(attribute))
                            category.ProductAttributes.Remove(attribute);
                    }
                }

                TryUpdateModel(category, new[] { 
                    "Name", 
                    "Title", 
                    "SortOrder", 
                    "SeoDescription", 
                    "SeoKeywords" });

                context.AddToCategory(category);
                context.SaveChanges();

                return RedirectToAction("Index", "Catalogue", new { Area = "", id = category.Name });
            }
        }
Esempio n. 3
0
 private static MenuItem CreateMenuItem(Category category, MenuItem parent, int level)
 {
     return new MenuItem
     {
         Title = category.Title,
         ContentId = category.Id,
         ContentName = category.Name,
         SortOrder = category.SortOrder,
         Level = level,
         Parent = parent,
         Children = new List<MenuItem>()
     };
 }
Esempio n. 4
0
        public ActionResult Create(Category model)
        {
            //ModelState.Clear();
            try
            {
                model.Text = HttpUtility.HtmlDecode(model.Text);
                model.Id = 0;
                Category parent = null;
                int categoryLevel = 0;
                if (model.CategoryId != null)
                {
                    parent = _context.Categories.First(c => c.Id == model.CategoryId);
                    categoryLevel = parent.CategoryLevel + 1;
                }



                var cache = new Category
                {
                    Name = SiteHelper.UpdatePageWebName(model.Name),
                    SortOrder = model.SortOrder,
                    Parent = parent,
                    CategoryLevel = categoryLevel,
                    Title = model.Title,
                    //Text = model.Text,
                    IsNewsCategory = model.IsNewsCategory

                    //CategoryId = model.CategoryId
                };

                if (_context.Categories.FirstOrDefault(c => c.Name == cache.Name) != null)
                {
                    throw new Exception("Каетгория с идентификатором " + cache.Name + " уже существует. Введите другой идентификатор");
                }

                model.Text = model.Text ?? "";

                _context.Categories.Add(cache);
                //_context.SaveChanges();

                var lang = _context.Languages.FirstOrDefault(p => p.Id == model.CurrentLang);
                if (lang != null)
                {
                    CreateOrChangeContentLang(_context, model, cache, lang);
                }

            }
            catch (DbEntityValidationException ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return View(model);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return View(model);
            }

            return RedirectToAction("Index");

        }
Esempio n. 5
0
 private static void Visit(Category node, MenuItem parent, int level)
 {
     if (node.Children == null || node.Children.Count == 0)
     {
         return;
     }
     foreach (var item in node.Children)
     {
         var child = CreateMenuItem(item,parent, level+1);
         parent.Children.Add(child);
         Visit(item, child, level + 1);
     }
 }
Esempio n. 6
0
 private void Visit(Category node)
 {
     _result.Add(node);
     if (node.Children == null || node.Children.Count == 0)
     {
         return;
     }
     foreach (var child in node.Children.OrderBy(c => c.SortOrder))
     {
         Visit(child);
     }
 }
Esempio n. 7
0
        private void CreateOrChangeContentLang(SiteContext context, Category instance, Category cache, Language lang)
        {

            CategoryLang categoryLang = null;
            if (cache != null)
            {
                categoryLang = context.CategoryLangs.FirstOrDefault(p => p.CategoryId == cache.Id && p.LanguageId == lang.Id);
            }
            if (categoryLang == null)
            {
                var newPostLang = new CategoryLang
                {
                    CategoryId = instance.Id,
                    LanguageId = lang.Id,
                    Title = instance.Title,
                    Text = instance.Text
                };
                context.CategoryLangs.Add(newPostLang);
            }
            else
            {
                categoryLang.Title = instance.Title;
                categoryLang.Text = instance.Text;
            }
            context.SaveChanges();

        }
Esempio n. 8
0
        public ActionResult Edit(Category model)
        {
            try
            {
                model.Text = HttpUtility.HtmlDecode(model.Text);
                var cache = _context.Categories.FirstOrDefault(p => p.Id == model.Id);

                if (cache != null)
                {
                    TryUpdateModel(cache, new[] { "SortOrder", "Title", "IsNewsCategory" });
                    cache.Name = SiteHelper.UpdatePageWebName(model.Name);

                    //if (_context.Categories.FirstOrDefault(c => c.Name == cache.Name) != null)
                    //{
                    //    throw new Exception("Каетгория с идентификатором " + cache.Name + " уже существует. Введите другой идентификатор");
                    //}

                    model.Text = model.Text ?? "";
                    var lang = _context.Languages.FirstOrDefault(p => p.Id == model.CurrentLang);
                    if (lang != null)
                    {
                        CreateOrChangeContentLang(_context, model, cache, lang);
                    }
                }
            }
            catch (DbEntityValidationException ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return View(model);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.Message;
                return View(model);
            }

            return RedirectToAction("Index");
        }
Esempio n. 9
0
 /// <summary>
 /// Create a new Category object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="title">Initial value of the Title property.</param>
 public static Category CreateCategory(global::System.Int32 id, global::System.String name, global::System.String title)
 {
     Category category = new Category();
     category.Id = id;
     category.Name = name;
     category.Title = title;
     return category;
 }
Esempio n. 10
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Category EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCategory(Category category)
 {
     base.AddObject("Category", category);
 }