有没有办法在更改任一字段时触发远程验证属性

本文关键字:程验证 属性 验证 字段 任一 有没有 | 更新日期: 2023-09-27 18:30:53

所以我面临的问题是,我创建的 Remote 属性仅在声明它的字段更改时触发。但是我需要它在更改任一时触发。

class MyViewModel
[Remote("ValidateTwoFields", "ControllerName", AdditionalFields = "Field2", ErrorMessageResourceName = "Errors_SomeErrorMessageThatShouldBeShowOnce", ErrorMessageResourceType = typeof(Resources.Resource))]
public string Field1{ get; set; }
public string Field2{ get; set; }

在控制器中:

//this only gets called when Field1 changes
//If I put the remote attribute on Field2 as well it displays the error message twice
public JsonResult ValidateTwoFields(MyViewModel model)
{
}

有没有办法在更改任一字段时触发远程验证属性

我还没有尝试过,但是可以通过将两个字段集中到一个包含两个字符串的类中并在一个类上添加验证来解决吗?