您的位置:首页 > 运维架构 > 网站架构

MVC网站开发:自定义扩展方法ModelStateExtension获取ModelState中的错误信息

2017-01-12 22:14 746 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Rw.Web.Core.Extension
{
public static class ModelStateExtension
{

/// <summary>
/// 获取模型绑定中的ErrMsg
/// </summary>
/// <param name="msDictionary"></param>
/// <returns></returns>
public static string GetErrMsg(this ModelStateDictionary msDictionary)
{
if (msDictionary.IsValid || !msDictionary.Any()) return "";
foreach (string key in msDictionary.Keys)
{
ModelState tempModelState = msDictionary[key];
if (tempModelState.Errors.Any())
{
var firstOrDefault = tempModelState.Errors.FirstOrDefault();
if (firstOrDefault != null)
return firstOrDefault.ErrorMessage;
}
}
return "";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐