本文整理汇总了C#中ModelStateDictionary.GetFirstError方法的典型用法代码示例。如果您正苦于以下问题:C# ModelStateDictionary.GetFirstError方法的具体用法?C# ModelStateDictionary.GetFirstError怎么用?C# ModelStateDictionary.GetFirstError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelStateDictionary
的用法示例。
在下文中一共展示了ModelStateDictionary.GetFirstError方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RetrieveErrors
/// <summary>
/// 收集校验错误信息
/// </summary>
/// <param name="modelState"></param>
/// <returns></returns>
protected virtual string RetrieveErrors(ModelStateDictionary modelState)
{
return modelState.GetFirstError();
//var errors = new Dictionary<string, string>();
//foreach (KeyValuePair<string, ModelState> keyValue in modelState)
//{
// if (keyValue.Value.Errors.Any())
// errors[keyValue.Key] = string.Join("; ", keyValue.Value.Errors.Select(e => (e.ErrorMessage != "" ? e.ErrorMessage : e.Exception.Message)));
//}
//var errorString = string.Join("|",
// errors.Select(x => !string.IsNullOrEmpty(x.Key) ?
// string.Format("{1}({0})", x.Key, x.Value) : string.Format("{1}", x.Key, x.Value)));
//return errorString;
}
示例2: HandleModelStateValidationError
/// <summary>
/// 处理模型校验错误
/// </summary>
/// <param name="modelState"></param>
/// <param name="result"></param>
/// <returns></returns>
private bool HandleModelStateValidationError(ModelStateDictionary modelState, out ActionResult result)
{
var msg = modelState.GetFirstError();
if (this.HttpContext.Request.IsAjaxRequest())
{
result = new JsonResult()
{
Data = new ApiResult(200, msg),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
return true;
}
result = null;
return false;
}