Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于jquery验证的DataAnnotations-一个字段在另一个字段上显示错误_C#_Asp.net Mvc_Data Annotations - Fatal编程技术网

C# 用于jquery验证的DataAnnotations-一个字段在另一个字段上显示错误

C# 用于jquery验证的DataAnnotations-一个字段在另一个字段上显示错误,c#,asp.net-mvc,data-annotations,C#,Asp.net Mvc,Data Annotations,如果我有这样一门课: class Sausage { [Required] public string Location {get;set;} [Required] public decimal Lat {get;set;} [Required] public decimal Lng {get;set;} } class Sausage { [Required] public string Location {get;set;

如果我有这样一门课:

class Sausage
{
    [Required]
    public string Location {get;set;}

    [Required]
    public decimal Lat {get;set;}

    [Required]
    public decimal Lng {get;set;}
}
class Sausage
{
    [Required]
    public string Location {get;set;}

    [Required]        
    [ErrorFor(Location, "The Location must be filled out")]
    public decimal Lat {get;set;}

    [Required]
    [ErrorFor(Location, "The Location must be filled out")]
    public decimal Lng {get;set;}
}
如果未填写
香肠.Lat
香肠.Lng
,我想在
香肠.Location
上填充一条错误消息。。。有没有一个简单的方法可以做到这一点

代码可能如下所示:

class Sausage
{
    [Required]
    public string Location {get;set;}

    [Required]
    public decimal Lat {get;set;}

    [Required]
    public decimal Lng {get;set;}
}
class Sausage
{
    [Required]
    public string Location {get;set;}

    [Required]        
    [ErrorFor(Location, "The Location must be filled out")]
    public decimal Lat {get;set;}

    [Required]
    [ErrorFor(Location, "The Location must be filled out")]
    public decimal Lng {get;set;}
}
我之前已经实现了这个接口来帮助解决这个问题

还有其他方法,如创建自定义验证属性,但我发现这对我来说更容易:

public class Sausage : IValidatableObject
{

    public string Location { get; set; }

    [Required]
    public decimal Lat { get; set; }

    [Required]
    public decimal Lng { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Lat == decimal.Zero || Lng == decimal.Zero)
        {
            yield return new ValidationResult("Please ensure both Lng & Lat is filled in", new[] { "Location" });
        }
    }
}
public类:IValidatableObject
{
公共字符串位置{get;set;}
[必需]
公共十进制Lat{get;set;}
[必需]
公共十进制数{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext)
{
如果(Lat==十进制.0 | | Lng==十进制.0)
{
收益率返回新的ValidationResult(“请确保填写Lng和Lat”,新[]{“位置”});
}
}
}

不幸的是,当Lat和Lng为空时,jquery验证不会出现错误,但是jquery验证失败,因为没有填写它们,因此用户无法保存表单。我忘记在回答中提到,Lat和Lng怎么可能为空?它们都设置为小数,而不是可为空的小数。也许可以发布html视图的代码。如果要使用自定义jquery验证,请查看:。它实际上是用于jquery验证方面的。。。所以在html中。。。这些值实际上可以为null。。。当它们通过控制器发布时,它们将被设置为0。。。我需要ASP new MVC中的jquery验证部分来理解如果它们实际上是空的。。。它应该在位置字段上显示错误。-客户端验证这在C#域(纯JavaScript域)中不是问题,如果您希望错误来自服务器端,那么您也应该考虑将元数据发布到表单中。