asp.net-mvc - 多个字段的远程验证

标签 asp.net-mvc asp.net-mvc-4

我想验证 Ville + Code-postal + Pays是独一无二的。

如果验证未通过,我想将字段标记为无效(通常为红色)。

enter image description here

我已经尝试了如下的第一个实现:

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/

相关文章:

asp.net - 剑道 TabStrip : Getting the Selected Index on the Selected event (MVC 4)

asp.net - 如何在 F# 中制作使用连字符的 Controller ?

c# - 尝试在没有 Entity Framework 的情况下使用表单发布——我放弃了,但会保持打开状态,以防它帮助某人......在某个地方

c# - 在 MVC 中显示文件夹中的所有图像。用一个foreach

c# - 覆盖 MVC 中的最大长度错误消息

javascript - 使用 javascript 管理 MVC 复选框

asp.net-mvc - 如何通过短信确认新用户注册?

c# - MVC 4 中的异步调用

javascript - 使用ajax显示列表

c# - Asp.Net MVC 4 中的自定义 URL 路由