public AjaxResult(System.Web.Mvc.ModelStateDictionary modelState, int id) { Result = modelState.IsValid; ErrorId = id; Errors = modelState.Where(kvp => kvp.Value.Errors.Count > 0).ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()).ToArray(); }
public AjaxResult(System.Web.Mvc.ModelStateDictionary modelState, int id, string customErrorMessage) { Result = false; CustomErrorMessage = customErrorMessage; ErrorId = id; Errors = modelState.Where(kvp => kvp.Value.Errors.Count > 0).ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()).ToArray(); }
public static bool CheckIfHasErrorsFor <TModel>(this System.Web.Mvc.ModelStateDictionary modelState, Expression <Func <TModel, object> > expression) { string expressionText = Helpers.GetExpressionText(expression); return(!string.IsNullOrEmpty(expressionText) && modelState.Where(item => item.Key.StartsWith(expressionText)) .Any(item => item.Value.Errors.Count != 0)); }
public static void ClearErrorsFor <TModel>(this System.Web.Mvc.ModelStateDictionary modelState, Expression <Func <TModel, object> > modelExpression) { string expressionText = Helpers.GetExpressionText(modelExpression); if (!string.IsNullOrEmpty(expressionText)) { modelState.Where(item => item.Key.Contains(expressionText)) .ToList() .ForEach(item => item.Value.Errors.Clear()); } }
public static void ClearErrorsFor <TModel, TException>(this System.Web.Mvc.ModelStateDictionary modelState, Expression <Func <TModel, object> > modelExpression) { string expressionText = Helpers.GetExpressionText(modelExpression); if (!string.IsNullOrEmpty(expressionText)) { modelState.Where(item => item.Key.Contains(expressionText)) .ToList() .ForEach(item => item.Value.Errors.Where(e => e.Exception != null && e.Exception.GetType() == typeof(TException)) .ToList() .ForEach(e => item.Value.Errors.Remove(e))); } }
/// <summary> /// MVC form validation messages for Angular /// </summary> /// <param name="modelState"></param> /// <returns></returns> public static IEnumerable <FormResponseFieldErrors> GetErrors(this System.Web.Mvc.ModelStateDictionary modelState) { List <FormResponseFieldErrors> errors = new List <FormResponseFieldErrors>(); foreach (var fieldErrors in modelState.Where(x => x.Value.Errors.Any()).Select(x => new { x.Key, x.Value.Errors })) { errors.Add( new FormResponseFieldErrors() { Field = fieldErrors.Key, ErrorMessages = fieldErrors.Errors.Select(x => x.ErrorMessage) }); } return(errors); }
/// <summary> /// 获取所有的错误的验证信息 /// </summary> /// <param name="modelState"></param> /// <returns></returns> public static IEnumerable <InputError> AllModelStateErrors(this System.Web.Mvc.ModelStateDictionary modelState) { var result = new List <InputError>(); //找到出错的字段以及出错信息 var errorFieldsAndMsgs = modelState.Where(m => m.Value.Errors.Any()) .Select(x => new { x.Key, x.Value.Errors }); foreach (var item in errorFieldsAndMsgs) { //获取键 var fieldKey = item.Key; //获取键对应的错误信息 var fieldErrors = item.Errors .Select(e => new InputError(fieldKey, e.ErrorMessage)); result.AddRange(fieldErrors); } return(result); }
public static bool CheckIfHasErrorsFor(this System.Web.Mvc.ModelStateDictionary modelState, string expressionText) { return((!modelState.IsValid || !string.IsNullOrEmpty(expressionText)) && modelState.Where(item => item.Key.StartsWith(expressionText)) .Any(item => item.Value.Errors.Count != 0)); }