Get error message if ModelState.IsValid fails?

前端 未结 11 1278
离开以前
离开以前 2020-11-30 02:27

I have this function in my controller.

[HttpPost]
public ActionResult Edit(EmployeesViewModel viewModel)
{
    Employee employee = GetEmployee(viewModel.Empl         


        
11条回答
  •  爱一瞬间的悲伤
    2020-11-30 02:51

    It is sample extension

    public class GetModelErrors
    {
        //Usage return Json to View :
        //return Json(new { state = false, message = new GetModelErrors(ModelState).MessagesWithKeys() });
        public class KeyMessages
        {
            public string Key { get; set; }
            public string Message { get; set; }
        }
        private readonly ModelStateDictionary _entry;
        public GetModelErrors(ModelStateDictionary entry)
        {
            _entry = entry;
        }
    
        public int Count()
        {
            return _entry.ErrorCount;
        }
        public string Exceptions(string sp = "\n")
        {
            return string.Join(sp, _entry.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.Exception));
        }
        public string Messages(string sp = "\n")
        {
            string msg = string.Empty;
            foreach (var item in _entry)
            {
                if (item.Value.ValidationState == ModelValidationState.Invalid)
                {
                    msg += string.Join(sp, string.Join(",", item.Value.Errors.Select(i => i.ErrorMessage)));
                }
            }
            return msg;
        }
    
        public List MessagesWithKeys(string sp = "

    ● ") { List list = new List(); foreach (var item in _entry) { if (item.Value.ValidationState == ModelValidationState.Invalid) { list.Add(new KeyMessages { Key = item.Key, Message = string.Join(null, item.Value.Errors.Select(i => sp + i.ErrorMessage)) }); } } return list; } }

提交回复
热议问题