如何手动在asp.net-mvc中检查模型有效性并获取详细错误消息?
- 内容介绍
- 文章标签
- 相关推荐
本文共计336个文字,预计阅读时间需要2分钟。
现在,我有一个这样的模型:`namespace TestMVC.Models.ViewModel{public partial class User {public string Name {get; set; }public int Age {get; set; }public string Email {get; set; }}[MetadataType(typeof(UserMetaData))]public User() {}}`
namespace TestMVC.Models.ViewModel { public partial class User { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } } [MetadataType(typeof(UserMetaData))] public partial class User { } public class UserMetaData { [Display(Name = "Name")] public string Name { get; set; } [Display(Name = "Age")] public int Age { get; set; } [Display(Name = "Email")] [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "The email is invalid")] public string Email { get; set; } } }
我有一个像这样的方法:
public ActionResult ValidCheck() { ModelState.AddModelError("error", "error"); Models.ViewModel.User model = new Models.ViewModel.User(); model.Age = 12; model.Name = "Andy He"; model.Email = "123"; //TryValidateModel(model); }
我想通过方法来检查模型是否有效并获取错误消息,我尝试使用TryValideModel但它只能获得模型有效的结果,无法获取错误消息,是否有方法可以做这个?你能帮帮我吗?
添加特定键的错误.像这样用.ModelState.AddModelError(“yourModelPropety”,“error”);在这里,您要为特定键设置模型错误.
使用ModelState.IsValid属性.它告诉您是否有任何模型错误已添加到ModelState. ModelState.Isvalid
试试这样.
ModelState.AddModelError("Email ", "error"); here you are setting model error for particular key. if(!ModelState.IsValid) { // do something to display errors . foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { DoSomethingWith(error); } } }
本文共计336个文字,预计阅读时间需要2分钟。
现在,我有一个这样的模型:`namespace TestMVC.Models.ViewModel{public partial class User {public string Name {get; set; }public int Age {get; set; }public string Email {get; set; }}[MetadataType(typeof(UserMetaData))]public User() {}}`
namespace TestMVC.Models.ViewModel { public partial class User { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } } [MetadataType(typeof(UserMetaData))] public partial class User { } public class UserMetaData { [Display(Name = "Name")] public string Name { get; set; } [Display(Name = "Age")] public int Age { get; set; } [Display(Name = "Email")] [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "The email is invalid")] public string Email { get; set; } } }
我有一个像这样的方法:
public ActionResult ValidCheck() { ModelState.AddModelError("error", "error"); Models.ViewModel.User model = new Models.ViewModel.User(); model.Age = 12; model.Name = "Andy He"; model.Email = "123"; //TryValidateModel(model); }
我想通过方法来检查模型是否有效并获取错误消息,我尝试使用TryValideModel但它只能获得模型有效的结果,无法获取错误消息,是否有方法可以做这个?你能帮帮我吗?
添加特定键的错误.像这样用.ModelState.AddModelError(“yourModelPropety”,“error”);在这里,您要为特定键设置模型错误.
使用ModelState.IsValid属性.它告诉您是否有任何模型错误已添加到ModelState. ModelState.Isvalid
试试这样.
ModelState.AddModelError("Email ", "error"); here you are setting model error for particular key. if(!ModelState.IsValid) { // do something to display errors . foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelState.Errors) { DoSomethingWith(error); } } }

