在更改任一字段时,是否有一种方法可以触发远程验证属性?

时间:2012-04-11 03:33:00

标签: c# .net asp.net-mvc-3 validation

所以我面临的问题是我创建的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)
{

}

1 个答案:

答案 0 :(得分:0)

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