我想验证 Ville
+ Code-postal
+ Pays
是独一无二的。
如果验证未通过,我想将字段标记为无效(通常为红色)。
我已经尝试了如下的第一个实现:
public class CityEditViewModel
{
public int CityID { get; set; }
[Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CountryID, CityID, PostCode", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
[Display(Name = "City", ResourceType = typeof(UserResource))]
public string CityName { get; set; }
[Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CountryID, CityID, CityName", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
[Display(Name = "PostCode", ResourceType = typeof(UserResource))]
public string PostCode { get; set; }
[Required, Remote("CityAlreadyExists", "City", AdditionalFields = "CityName, PostCode, CityID", ErrorMessageResourceName = "CityAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
[Display(Name = "Country", ResourceType = typeof(UserResource))]
public int CountryID { get; set; }
public List<SelectListItem> Countries { get; set; }
}
但是直到我真正更改其中的某些内容时,才会检查(验证)所有字段。我需要一个解决方案,每次更改 3 个字段中的一个时,所有 3 个字段都会被验证并在需要时标记为红色。
我已经检查了其他 Stackoverflow 帖子,但没有找到解决我的具体问题的方法。
谢谢你的帮助。
最佳答案
我也有类似的情况,这个帮助我开始
$("#FirstName").change(function () {
$('#CardNumber').removeData('previousValue');
$('#CardNumber').valid();
});
Multiple fields validation using Remote Validation
关于asp.net-mvc - 多个字段的远程验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777603/