【问题标题】:Is there a way for a remote validation attribute to fire when either field is changed?有没有办法在更改任一字段时触发远程验证属性?
【发布时间】:2012-04-11 03:33:00
【问题描述】:

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

}

【问题讨论】:

  • 我之前处理过类似的问题,我处理它的方式是在第二个字段上使用一些 javascript(即处理一些事件并触发验证)。希望其他人有更简单的解决方案。
  • 如何将您的属性也设置为第二个字段并将 AdditionalFields 设置为 Field1 呢?
  • 这会导致消息显示两次,并且当第二个更改时,第一个的错误消息不会消失。它们被视为单独的实例。
  • 计算选项可能是 1) 滚动我自己的支持 AllowMultiple 的属性或 2) Javascript 客户端手动触发它
  • appclay 你能发布你的解决方案吗?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2013-11-09
    • 2013-06-14
    • 1970-01-01
    • 2022-07-26
    相关资源
    最近更新 更多