Пример #1
0
        public ActionResult Create(CampusViewModel viewModel)
        {
            try
            {
                string MatchPhonePattern = @"^\(?([0-9]{3})\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$";
                Regex  rx           = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var    validateName = _campusService.GetCampusDuplicate(viewModel.ID, viewModel.Name);
                if (validateName != null)
                {
                    ModelState.AddModelError("Name", "Campus Name already exists");
                    return(View(viewModel));
                }
                else
                {
                    try
                    {
                        if (!rx.IsMatch(viewModel.Telephone.Trim()))
                        {
                            ModelState.AddModelError("Telephone", "Telephone must numbers");
                            return(View(viewModel));
                        }

                        var campus = new Campus()
                        {
                            Name      = viewModel.Name.Trim(),
                            Address   = viewModel.Address,
                            Telephone = viewModel.Telephone,
                            Active    = true
                        };

                        _campusService.Add(campus);
                        _campusService.SaveChanges();
                        SetAlert("Add Campus success", "success");
                    } catch (Exception e)
                    {
                        return(View(viewModel));
                    }
                }

                if (!ModelState.IsValid)
                {
                    // xy ly loi
                    return(View(viewModel));
                }
            }
            catch (Exception e)
            {
                SetAlert("Add Campus error", "error");
            }
            return(RedirectToAction("Index"));
        }