You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
904 B
C#

using System.Text;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc
{
public static class ModelStateExtensions
{
/// <summary>
/// 获取模型验证内的所有错误信息
/// </summary>
/// <param name="modelState">模型验证对象</param>
/// <returns></returns>
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();
}
}
}