相关疑难解决方法(0)

mvc4数据注释比较两个日期

我的模型中有这两个字段:

[Required(ErrorMessage="The start date is required")]
[Display(Name="Start Date")]
[DisplayFormat(DataFormatString = "{0,d}")]
public DateTime startDate { get; set; }

[Required(ErrorMessage="The end date is required")]
[Display(Name="End Date")]
[DisplayFormat(DataFormatString = "{0,d}")]
public DateTime endDate{ get; set; }
Run Code Online (Sandbox Code Playgroud)

我要求endDate必须大于startDate.我试过使用,[Compare("startDate")]但这只适用于相同的操作.

我应该用什么来做"大于"的操作?

asp.net-mvc data-annotations asp.net-mvc-4

30
推荐指数
2
解决办法
5万
查看次数

MVC4和IClientValidatable-服务器端验证的自动AJAX调用

我希望在MVC4中实现自定义客户端验证。我目前使用标准属性(例如在我的模型中)

public class UploadedFiles
{
    [StringLength(255, ErrorMessage = "Path is too long.")]
    [Required(ErrorMessage = "Path cannot be empty.")]
    [ValidPath]
    public string SourceDirectory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

因此,StringLength和Required都自动转换为某些JQuery客户端验证。当前,“有效路径”仅在服务器端起作用。始终需要在服务器端进行验证,因为只有服务器才能验证路径是否有效,而您无法在客户端进行验证。

服务器端代码看起来像

public class ValidPathAttribute : ValidationAttribute, IClientValidatable
{
    public string SourceDirectory;

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string path = value.ToString();
        string message = string.Empty;

        var fileSystemSupport = new FileSystemSupport(Settings, new WrappedFileSystem(new FileSystem()));

        if (fileSystemSupport.ValidateNetworkPath(path, out message))
        {
            return ValidationResult.Success;
        }

        return new ValidationResult(message);
    }
} 
Run Code Online (Sandbox Code Playgroud)

这很好。现在,我希望通过ajax调用来实现,进入“ IClientValidatable”和“ GetClientValidationRules”。我写完书之后

public …
Run Code Online (Sandbox Code Playgroud)

validation ajax jquery asp.net-mvc-4

3
推荐指数
1
解决办法
1996
查看次数