Example #1
0
        // GET : Township/New
        public ActionResult New()
        {
            var viewModel = new TownshipViewModel
            {
                Cities = _cityServcies.GetAll()
            };

            return(View("TownshipForm", viewModel));
        }
Example #2
0
        public ActionResult Update(TownshipViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId();
                _townshipServices.Update(viewModel, userId);
            }

            return(View("TownshipForm", viewModel));
        }
Example #3
0
        public ActionResult Create(TownshipViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId();
                _townshipServices.Create(viewModel, userId);

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.Title = _townshipServices.GenerateViewBagTitle(viewModel.Id);

            return(View("TownshipForm", viewModel));
        }
Example #4
0
        // GET : Township/Edit/{id}
        public ActionResult Edit(int id)
        {
            var township = _townshipServices.GetById(id);

            if (township != null)
            {
                var viewModel = new TownshipViewModel
                {
                    Id       = township.Id,
                    Name     = township.Name,
                    CityId   = township.CityId,
                    CityName = township.CityName,
                    Cities   = _cityServcies.GetAll()
                };
                ViewBag.Title = _townshipServices.GenerateViewBagTitle(id);

                return(View("TownshipForm", viewModel));
            }

            return(HttpNotFound("Sorry, looks like invalid township Id."));
        }
Example #5
0
        public async Task <IActionResult> Index(int StateId, int CountryId, string sortParam, string select, string searchParam, string searchParam1, int studentPage = 1, int PageSize = 3)
        {
            TownshipViewModel TownshipVM = new TownshipViewModel()
            {
                Townships = new List <Models.Township>()
            };

            FillCountries(CountryId);

            FillStates(CountryId);
            if (CountryId == 0 || ViewBag.States.Count <= 1)
            {
                ViewBag.StateEnabled = false;
                StateId = 0;
            }

            TownshipVM.Townships = await db.Townships.ToListAsync();

            if (StateId != 0)
            {
                ViewBag.selectStateId = "StateId";
            }

            if (searchParam == null)
            {
                if (CountryId != 0 && StateId == 0)
                {
                    TownshipVM.Townships = (db.Townships.Where(a => a.State.CountryId == CountryId).ToList());
                }
                if (CountryId == 0 && StateId != 0)
                {
                    TownshipVM.Townships = (db.Townships.Where(a => a.StateId == StateId).ToList());
                }
                if (CountryId != 0 && StateId != 0)
                {
                    TownshipVM.Townships = (db.Townships.Where(a => a.State.CountryId == CountryId).ToList()).Where(b => b.StateId == StateId).ToList();
                }
            }
            else
            {
                if (CountryId != 0 && StateId == 0)
                {
                    TownshipVM.Townships = (db.Townships.Where(a => a.State.CountryId == CountryId).ToList()).Where(b => b.TownshipName.ToLower().Contains(searchParam.ToLower())).ToList();
                    if (TownshipVM.Townships.Count == 0)
                    {
                        studentPage = 0;
                    }
                }
                if (CountryId != 0 && StateId != 0)
                {
                    TownshipVM.Townships = ((db.Townships.Where(a => a.State.CountryId == CountryId).ToList()).Where(b => b.StateId == StateId).ToList()).Where(b => b.TownshipName.ToLower().Contains(searchParam.ToLower())).ToList();
                    if (TownshipVM.Townships.Count == 0)
                    {
                        studentPage = 0;
                    }
                }
                if (CountryId == 0 && StateId == 0)
                {
                    TownshipVM.Townships = db.Townships.ToList().Where(b => b.TownshipName.ToLower().Contains(searchParam.ToLower())).ToList();
                }
            }

            StringBuilder param = new StringBuilder();

            param.Append("/Test?studentPage=:");


            param.Append("&select=");
            if (select != null)
            {
                param.Append(select);
                ViewBag.selectParam = select;
            }
            param.Append("&CountryId=");
            if (CountryId != 0)
            {
                param.Append(CountryId);
            }

            param.Append("&StateId=");
            if (StateId != 0)
            {
                param.Append(StateId);
            }

            param.Append("&searchParam=");
            if (searchParam != null)
            {
                param.Append(searchParam);
            }


            param.Append("&sortParam=");

            if (sortParam != null)
            {
                param.Append(sortParam);
            }
            //param.Append("&CountryId=");
            //if (CountryId != 0)
            //{
            //    param.Append(CountryId);
            //}

            if (PageSize <= 0)
            {
                PageSize = 9;
            }

            ViewBag.PageSize = PageSize;


            param.Append("&PageSize=");
            if (PageSize != 0)
            {
                param.Append(PageSize);
            }


            var count = TownshipVM.Townships.Count;

            if (count == 0)
            {
                studentPage = 0;
            }


            if (sortParam == "SortDec")
            {
                TownshipVM.Townships = TownshipVM.Townships.OrderByDescending(p => p.TownshipName)
                                       .Skip((studentPage - 1) * PageSize)
                                       .Take(PageSize).ToList();
                ViewBag.sortParamView = "SortDec";
            }


            else
            {
                TownshipVM.Townships = TownshipVM.Townships.OrderBy(p => p.TownshipName)
                                       .Skip((studentPage - 1) * PageSize)
                                       .Take(PageSize).ToList();
                ViewBag.sortParamView = "SortAsc";
            }



            TownshipVM.PagingInfo = new PagingInfo()
            {
                CurrentPage  = studentPage,
                ItemsPerPage = PageSize,
                TotalItems   = count,
                urlParam     = param.ToString(),
                //PagerSize = 5,
            };

            //if(country!=0)
            //{
            //    return View("Index");
            //}

            return(View(TownshipVM));
        }