ASP.NET MVC2 - 在服务器端验证之前从表单提交修剪空格?

3Da*_*ave 3 validation attributes trim asp.net-mvc-2

如果我添加验证属性:

public class ProductDownloadListModel
{        
    //xxxxx-xxxxx-xxxxx
    [Required]
    [StringLength(17)]
    public string PSN { get; set; }
    public DateTime PsnExpirationDate { get; set; } 
    public DataTable Downloads { get; set; } 

}
Run Code Online (Sandbox Code Playgroud)

并且用户输入一个17个字符的字符串,但最后包含空格,我得到验证错误,因为字符串大于[StringLength(17)]属性指定的字符串.我怎么能阻止这个?我希望在提交之前不必让javaScript修剪字符串.

Jes*_*och 5

对你的PSN进行修剪.

public string PSN
{
    get
    {
        return (this.psn == null) ? string.Empty : this.psn;
    }
    set
    {
        this.psn = (value == null || value.Trim().Length == 0) ? null :value.Trim();
    }
}
Run Code Online (Sandbox Code Playgroud)