示例#1
0
 /* CREATE
  * ==========================================================================
  */
 public ActionResult Create()
 {
     var model = new ArticleCategoriesViewModel()
     {
         Categories = new MultiSelectList(Adapter.CategoryRepository.GetAll(), "ID", "Title"),
         Article = new Article()
     };
     return View(model);
 }
示例#2
0
 public ActionResult Create(ArticleCategoriesViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         var model = viewModel.Article;
         model.CreatedDate = DateTime.UtcNow;
         var ids = viewModel.SelectedCategoriesIds;
         if (ids != null && ids.Length > 0)
         {
             var categories = new List<Category>();
             foreach (var id in ids)
             {
                 var category = Adapter.CategoryRepository.Single(c => c.ID.Equals(id), null);
                 categories.Add(category);
             }
             model.Categories = categories;
         }
         Adapter.ArticleRepository.Insert(model);
         Adapter.Save();
         return RedirectToAction("Index");
     }
     return View(viewModel);
 }
示例#3
0
        public ActionResult Edit(ArticleCategoriesViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                //GET ORGINAL MODEL
                var modelOrginal = Adapter.ArticleRepository.Single(a => a.ID.Equals(viewModel.Article.ID), null);
                //ADD NEW VALUES
                modelOrginal.ModifiedDate = DateTime.UtcNow;
                modelOrginal.Title = viewModel.Article.Title;
                modelOrginal.Description = viewModel.Article.Description;
                modelOrginal.Body = viewModel.Article.Body;
                var ids = viewModel.SelectedCategoriesIds;
                if (ids != null && ids.Length > 0)
                {
                    if (modelOrginal.Categories != null)
                    {
                        modelOrginal.Categories.Clear();
                    }
                    var categories = new List<Category>();
                    foreach (var id in ids)
                    {
                        var category = Adapter.CategoryRepository.Single(c => c.ID.Equals(id), null);
                        categories.Add(category);
                    }
                    modelOrginal.Categories = categories;
                }
                Adapter.ArticleRepository.Update(modelOrginal);
                Adapter.Save();

                return RedirectToAction("Index");
            }
            return View(viewModel);
        }
示例#4
0
 /* UPDATE OR EDIT
  * ==========================================================================
  */
 public ActionResult Edit(int id)
 {
     var article = Adapter.ArticleRepository.Single(a => a.ID.Equals(id), null);
     long[] ids = null;
     if(article.Categories != null && article.Categories.Count > 0){
         ids = new long[article.Categories.Count];
         int i = 0;
         foreach (var category in article.Categories)
         {
             ids[i] = category.ID;
             i++;
         }
     }
     var model = new ArticleCategoriesViewModel()
     {
         SelectedCategoriesIds = ids,
         Categories = new MultiSelectList(Adapter.CategoryRepository.GetAll(), "ID", "Title"),
         Article = article
     };
     return View(model);
 }