简体   繁体   English

使用其他ViewModel的MVC验证

[英]MVC validation of with a different ViewModel

I have written a T4 template which automatically generates all the validations like this: 我编写了一个T4模板,该模板会自动生成所有这样的验证:

public class UserValidator : AbstractValidator<User>
{
    public UserValidator()
    {
        RuleFor(x => x.Email).NotEmpty();
        RuleFor(x => x.Email).Length(0, 150).WithMessage("Email can not exceed 150 characters");
        RuleFor(x => x.Password).NotEmpty();
        RuleFor(x => x.Password).Length(0, 50).WithMessage("Password can not exceed 50 characters");
        RuleFor(x => x.Name).NotEmpty();
        RuleFor(x => x.Name).Length(0, 100).WithMessage("Name can not exceed 100 characters");
        RuleFor(x => x.PhoneNumber).Length(0, 50).WithMessage("PhoneNumber can not exceed 50 characters");
        RuleFor(x => x.Address).Length(0, 50).WithMessage("Address can not exceed 50 characters");
        RuleFor(x => x.Postcode).Length(0, 50).WithMessage("Postcode can not exceed 50 characters");
        RuleFor(x => x.City).Length(0, 50).WithMessage("City can not exceed 50 characters");
        RuleFor(x => x.Country).Length(0, 100).WithMessage("Country can not exceed 100 characters");
        RuleFor(x => x.UserRoleId).NotNull();
    }
}

Now I have a ViewModel where I want to use these validations as such: 现在,我有一个ViewModel,我想在其中使用这些验证:

[Validator(typeof(UserValidator))]
public class RegisterModel
{
    public string Email { get; set; }

    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Compare("Password"), Display(Name = "Confirm Password"), DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }

    public string Name { get; set; }

    [Display(Name = "Phone Number")]
    public string PhoneNumber { get; set; }
    public string Address { get; set; }
    public string Postcode { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

My class is different from the one used in AbstractValidator, I have a RegisterModel class. 我的类不同于AbstractValidator中使用的类,我有一个RegisterModel类。

When I run I get the following error: 当我运行时,出现以下错误:

Unable to cast object of type 'Web.Models.RegisterModel' to type 'DAL.User'. 无法将类型为“ Web.Models.RegisterModel”的对象转换为类型为“ DAL.User”的对象。

...which makes sense as its not the same type. ...这是合理的,因为其类型不同。

I also tried using this way: 我也尝试过使用这种方式:

public class RegisterModel
{
    [Validator(typeof(UserValidator))]
    public User UserDetails { get; set; }

    [Compare("Password"), Display(Name = "Confirm Password"), DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
}

But can not use in this was as validator is a class level attribute and not property attribute. 但是不能在此使用,因为验证器是类级别的属性,而不是属性的属性。

Is there any way I can use existing Rules generated for my entity classes in the models? 有什么方法可以使用为模型中的实体类生成的现有规则?

Anyone tried something similar? 有人尝试过类似的东西吗? Or have any other ideas of this? 或对此有其他想法吗?

Obviously one of the solutions would be to inherit from User class: 显然,解决方案之一是从User类继承:

[Validator(typeof(UserValidator))]
public class RegisterModel : User
{
    [Compare("Password"), Display(Name = "Confirm Password"), DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
}

This makes sense if registration model extends user. 如果注册模型扩展了用户,这是有意义的。

Ok - then based on your comments and EF creating the classes for you: 好的-然后根据您的评论和EF为您创建类:

EF will create partial classes. EF将创建部分类。 If those live in the namespace "MyProject.Core" simply add the following almost empty class at some place and use the same namespace: 如果它们位于命名空间“ MyProject.Core”中,则只需在某个位置添加以下几乎为空的类并使用相同的命名空间即可:

public partial class User : IUserModel
{
    /* nothing goes in here */
}

public interface IUserModel
{
   /*all common properties go here*/
}

The [Required] attributes etc. can only live on the (View) Model and not on the interface. [Required]属性等只能存在于(视图)模型上,而不能存在于界面上。

Then you have to adjust your T4 template to not use "User" but "IUserModel" 然后,您必须调整T4模板以不使用“用户”,而使用“ IUserModel”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM