MVC 模型过滤
通用返回模型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// WebApi返回数据的泛型类 /// </summary> /// <typeparam name="T"></typeparam> public partial class VMResult<T> { public VMResult() { if (typeof(T).IsClass && typeof(T) != typeof(string)) { Data = System.Activator.CreateInstance<T>(); } } public VMResult(int pErrCode, string pResultMsg, T pData) { ErrCode = pErrCode; ResultMsg = pResultMsg; Data = pData; } int _ErrCode; /// <summary> /// 0成功,非0错误 /// </summary> public int ErrCode { get { return _ErrCode ;} set { _ErrCode = value; } } string _ResultMsg; /// <summary> /// 文字描述信息 /// </summary> public String ResultMsg { get { return _ResultMsg; } set { _ResultMsg = value; } } /// <summary> /// 泛型自定义数据 /// </summary> T _Data; public T Data { get { return _Data; } set { _Data = value; } } }
模型验证:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var result = new VMResult<string>(); if (!ModelState.IsValid) { result.ResultMsg = string.Join(",", ModelState.Values.SelectMany(m => m.Errors).Select(m => m.ErrorMessage)); result.ErrCode = 1; return result; }
https://www.cnblogs.com/Leo_wl/p/6798276.html