if (!ModelState.IsValid)
{
List<string> sb = new List<string>();
//获取所有错误的Key
List<string> Keys = ModelState.Keys.ToList();
//获取每一个key对应的ModelStateDictionary
foreach (var key in Keys)
{
var errors = ModelState[key].Errors.ToList();
//将错误描述添加到sb中
foreach (var error in errors)
{
sb.Add(error.ErrorMessage);
}
}
return Json(sb);
}
手动构造error
ModelState.AddModelError("UserName", "请输入用户名。");