Esempio n. 1
0
        public ActionResult Create(OwnerFormStub model)
        {
            var currentDate = DateTime.Now;

            model.CreatedBy   = User.Identity.Name;
            model.CreatedTime = currentDate;
            model.UpdatedBy   = User.Identity.Name;
            model.UpdatedTime = currentDate;

            bool checkUniqueness = RepoOwner.CheckCodeUniqueness(model.Code);

            if (checkUniqueness == false)
            {
                ModelState.AddModelError("Code", "Code sudah pernah digunakan sebelumnya");
            }

            //Entah kenapa validation failed terus klo ga di buat seperti ini
            ModelState.Remove("CreatedBy");
            ModelState.Remove("UpdatedBy");

            if (ModelState.IsValid)
            {
                owner dbItem = new owner();
                dbItem = model.GetDbObject();

                RepoOwner.Save(dbItem);

                //message
                string template = HttpContext.GetGlobalResourceObject("MyGlobalMessage", "CreateSuccess").ToString();
                this.SetMessage(model.Name, template);

                return(RedirectToAction("Index"));
            }
            else
            {
                //var errors = ModelState.Select(x => x.Value.Errors)
                //           .Where(y => y.Count > 0)
                //           .ToList();
                return(View("Form", model));
            }
        }