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 "";
}
}
}
MVC网站开发:自定义扩展方法ModelStateExtension获取ModelState中的错误信息
最新推荐文章于 2023-02-17 16:59:58 发布