Exemplo n.º 1
0
        public async Task <ActionResult> Edit(UserDetails Model)
        {
            var results = new ServerResponse();

            try
            {
                results = await userService.UpdateAsync(Model);
            }
            catch (FaultException ex)
            {
                var arr = ex.Message.Split('\n').ToArray();

                if (arr[0].Contains("validation errors"))
                {
                    arr = arr.Skip(2).ToArray();

                    foreach (var error in arr)
                    {
                        if (!string.IsNullOrWhiteSpace(error.Trim()))
                        {
                            var key = error.Split(' ')[1].TrimEnd('.');

                            ModelState.AddModelError(key, error.TrimEnd());
                        }
                    }

                    TempData["UserErrors"] = ModelState;
                    return(new HttpStatusCodeResult(422, "Validation Errors."));
                }
                else
                {
                    return(Json(results, JsonRequestBehavior.AllowGet));
                }
            }

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