public async Task <JsonResult> Create_Answer(TestQuestionAnswerDTO model) { string error = null; int id = 0; var errorList = ModelState.Where(n => n.Value.Errors.Any()).ToDictionary( kvp => kvp.Key, kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray() ); try { if (!ModelState.IsValid && errorList.Count > 0) { throw new Exception(string.Join(',', errorList.First().Value)); } id = await TestService.Answer_AddAsync(model); } catch (Exception ex) { error = ex.Message; } return(Json(new { id, error, errorList })); }