Exemplo n.º 1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id, Name, Description, Active, ActiveFrom, ActiveTo")] ApplicationResource resource)
        {
            // https://stackoverflow.com/questions/39533599/mvc-5-with-bootstrap-modal-from-partial-view-validation-not-working
            // https://stackoverflow.com/questions/2845852/asp-net-mvc-how-to-convert-modelstate-errors-to-json
            var retVal = new ResourceVm
            {
                Success = false,
                Message = ""
            };

            if (ModelState.IsValid)
            {
                var repo = _unitOfWorkAsync.RepositoryAsync <ApplicationResource>();

                var valid = resource.Validate();

                try
                {
                    repo.Update(resource);

                    var result = await _unitOfWorkAsync.SaveChangesAsync();

                    if (result > 0)
                    {
                        retVal.Success = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Debug.WriteLine(e);
                    retVal.Message = e.Message;
                }
            }
            else
            {
                retVal.FormErrors = ModelState.Select(kvp => new { key = kvp.Key, errors = kvp.Value.Errors.Select(e => e.ErrorMessage) });
                retVal.Message    = "Model state invalid";
            }

            return(PartialView("Partial/_resourceEditPartial", retVal));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Insert(ApplicationResource resourceToInsert)
        {
            var retVal = new ResourceInsertedVm {
                Success = false
            };

            if (ModelState.IsValid)
            {
                if (ModelState.Values.Any(i => i.Errors.Count > 0))
                {
                    var problems = ModelState.Values.Where(i => i.Errors.Count > 0).ToList();
                }
            }

            var res = new ApplicationResource
            {
                Name        = resourceToInsert.Name,
                Description = resourceToInsert.Description,
                Active      = resourceToInsert.MakeActive,
                ActiveFrom  = DateTime.Now,
                ActiveTo    = resourceToInsert.ActiveUntil
            };

            var validationResults = res.Validate();

            if (validationResults != null && validationResults.Any())
            {
                retVal.Success = false;

                var sb = new StringBuilder();

                foreach (var validation in validationResults)
                {
                    sb.Append(validation.ErrorMessage);
                }

                ModelState.AddModelError("Name", sb.ToString());
                retVal.ValidationIssues = sb.ToString();

                return(Json(retVal, JsonRequestBehavior.AllowGet));
            }

            var inserted = -1;

            try
            {
                _unitOfWorkAsync.RepositoryAsync <ApplicationResource>().Insert(res);
                inserted = await _unitOfWorkAsync.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Debug.WriteLine(e);
                _errorLogService.LogError(this, e.Message, e);
                retVal.Message = e.Message ?? "";
            }

            retVal.WasInserted = inserted;
            retVal.Success     = true;

            return(Json(retVal, JsonRequestBehavior.AllowGet));
        }