现在,我有一个这样的模型:
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 但它只能得到模型有效的结果,无法得到错误消息,有没有方法可以做这个?你能帮帮我吗?