using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using FluentValidation.Results;
namespace Pcb.Common.Extension
{
public static class ValidationResultExtensition
{
///
/// 添加错误信息到ModelState的Error集合。
///
/// ValidationResult
/// System.Web.Mvc.ModelStateDictionary
/// True:使用属性名,False:不用属性名
public static void AppendToModelStateError(this ValidationResult source, ModelStateDictionary modelState, bool isExposeProperty = false)
{
source.Errors.ToList()
.ForEach(
error =>
{
modelState.AddModelError(isExposeProperty ? error.PropertyName : "", error.ErrorMessage);
});
}
public static object ToErrorMsgList(this IList erroes, ModelStateDictionary modelState)
{
var list = erroes.Select(d => new { PropertyName = d.PropertyName, ErrorMessage = d.ErrorMessage }).ToList();
var list2 = modelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { PropertyName = x.Key, ErrorMessage = x.Value.Errors[0].ErrorMessage }).ToList();
list.AddRange(list2);
return list;
}
public static object ToErrorMsgList(this ModelStateDictionary modelState)
{
var list = modelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { PropertyName = x.Key, ErrorMessage = x.Value.Errors[0].ErrorMessage }).ToList();
return list;
}
}
}