1. 程式人生 > >MVC model驗證 獲取驗證錯誤信息

MVC model驗證 獲取驗證錯誤信息

錯誤 lstat ror ons 獲取 public exp foreach model

public static class ModelStateExtensions
    {
        /// <summary>
        /// 獲取model驗證錯誤信息
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static string ExpendErrors(this System.Web.Mvc.Controller controller)
        {
            StringBuilder strBuild 
= new StringBuilder(); foreach (var item in controller.ModelState.Values) { if (item.Errors.Count > 0) { int itemErrorCount = item.Errors.Count; for (int i = 0; i < itemErrorCount; i++) { strBuild.Append(item.Errors[i].ErrorMessage); strBuild.Append(
"<br/>"); } } } return strBuild.ToString(); } }

MVC model驗證 獲取驗證錯誤信息