ASP.NET MVC3 Eager Client-Validation,FluentHTML和嵌套ViewModels

时间:2011-06-18 09:40:10

标签: asp.net-mvc-3 jquery-validate viewmodel unobtrusive-javascript

我正在使用FluentHTML(来自MvcContrib)来布局我的HTML标记。我想使用jquery.validate库提供的急切的不引人注目的客户端验证。除了嵌套的ViewModel的属性之外,我得到了一切正常工作。例如:

public class RegisterPageViewModel
{
    [Required(ErrorMessage = "First Name cannot be empty.")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name cannot be empty.")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Email cannot be empty.")]
    [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Invalid Email Format.")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Password field cannot be empty.")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required(ErrorMessage = "You have to confirm your password.")]
    [Compare("Password", ErrorMessage = "Passwords must match.")]
    [DataType(DataType.Password)]
    public string PasswordConfirm { get; set; }

    [Required]
    public AddressDto Address { get; set; }

    public bool TermsOfUse { get; set; }

    [Required(ErrorMessage = "Nickname is required")]
    public string Nickname { get; set; }

    public string MiddleName { get; set; }

    [Required(ErrorMessage = "Birthdate is required")]
    public string Birthdate { get; set; }

    public string Phone { get; set; }

    [Required(ErrorMessage = "Mobile number is required")]
    public string Mobile { get; set; }

    public string RakimMelieh { get; set; }

    public string ReferralNickname { get; set; }
}

它适用于除Address属性内的所有属性,但我也使用验证属性修饰了AddressDto的属性:

public class AddressDto
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Address Name is required.")]
    public string Name { get; set; }
    public string Country { get; set; }
    public string District { get; set; }
    public string City { get; set; }
    public string Area { get; set; }
    [Required(ErrorMessage = "Address Details are required.")]
    public string Details { get; set; }
    public bool IsDefault { get; set; }
}

对于其他视图模型也会发生同样的事情,其中​​包含嵌套视图模型。检查输入字段FireBug时我注意到的一件事是它们总是有valid类,即使它们不是真正有效(根据装饰其属性的注释)。

我有什么想法可以解决这个问题吗?

0 个答案:

没有答案