using System.Text;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc
{
public static class ModelStateExtensions
{
///
/// 获取模型验证内的所有错误信息
///
/// 模型验证对象
///
public static string GetErrorMessage(this ModelStateDictionary modelState)
{
ArgumentException.ThrowIfNullOrEmpty(nameof(modelState));
StringBuilder sb = new();
foreach (var key in modelState.Keys)
{
var entry = modelState[key];
if (entry.ValidationState == ModelValidationState.Invalid)
{
sb.Append(string.Join("; ", entry.Errors));
}
}
return sb.ToString();
}
}
}