public ActionResult Create()
 {
     var model = new NavigationModel();
     model.CreatedDate = DateTime.Now;
     model.Published = true;
     PrepareAllNavigationsModel(model);
     return View(model);
 }
 public ActionResult Edit(int Id)
 {
     var entity = _navigationService.GetById(Id);
     var model = new NavigationModel();
     if (entity != null)
     {
         Mapper.CreateMap<Navigation, NavigationModel>();
         Mapper.Map(entity, model);
     }
     PrepareAllNavigationsModel(model);
     return View(model);
 }
 public ActionResult List()
 {
     var entities = _navigationService.Table.Where(x => !x.Deleted).OrderBy(x=>x.SortOrder);
     IList<NavigationModel> models = new List<NavigationModel>();
     Mapper.CreateMap<Navigation, NavigationModel>();
     foreach (var item in entities)
     {
         var nm = new NavigationModel();
         Mapper.Map(item, nm);
         nm.Name = GetName(item);
         models.Add(nm);
     }
     
     return View(models);
 }
 public ActionResult CreateOrUpdate(NavigationModel model)
 {
     var entity = model.Id > 0 ? _navigationService.GetById(model.Id) : new Navigation();
     Mapper.CreateMap<NavigationModel, Navigation>();
     Mapper.Map(model, entity);
     entity.LastUpdated = DateTime.Now;
     string msg = string.Empty;
     if (entity.Id == 0)
     {
         _navigationService.Insert(entity);
         msg = "Thêm menu thành công !";
     }
     else
     {
         msg = "Cập nhật menu thành công !";
         _navigationService.Update(entity);
     }
     TempData["Message"] = null;
     if (!String.IsNullOrEmpty(msg))
         TempData["Message"] = msg;
     return RedirectToAction("Edit", new { Id = entity.Id });
 }
        private void PrepareAllNavigationsModel(NavigationModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            model.AvailableNavigations.Add(new SelectListItem
            {
                Text = "[None]",
                Value = "0"
            });
            IQueryable<Navigation> navigations = _navigationService.Table.Where(x => x.Published && !x.Deleted);
            foreach (Navigation c in navigations)
            {
                if (model.Id == c.Id)
                    continue;
                model.AvailableNavigations.Add(new SelectListItem
                {
                    Text = GetName(c),
                    Value = c.Id.ToString(CultureInfo.InvariantCulture)
                });
            }
        }