我有两个字段,如电话号码和手机号码.就像是..
[Required]
public string Phone { get; set; }
[Required]
public string Mobile{ get; set; }
Run Code Online (Sandbox Code Playgroud)
但是用户可以在其中任何一个中输入数据.一个是强制性的.如何处理它们,即当用户在另一个字段中输入数据时如何禁用一个字段所需的字段验证器,反之亦然.在哪种情况下我必须在javascript中处理它以及我需要为此添加什么脚本.任何人都可以帮助找到解决方案......
我想收集放置在Property上的所有自定义属性.分配给属性的同一类型有多个属性,但在收集它们时,结果集合仅包含特定类型的第一个属性:
Attribute类
[AttributeUsage(System.AttributeTargets.Property,
AllowMultiple = true)]
public class ConditionAttribute : Attribute{...}
Run Code Online (Sandbox Code Playgroud)
用法:
[ConditionAttribute("Test1")]
[ConditionAttribute("Test2")]
[ConditionAttribute("Test3")]
public Color BackColor{get; set;}
Run Code Online (Sandbox Code Playgroud)
现在循环遍历对象'value'的所有Props,其类包含Prop"BackColor":
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value))
{
foreach (Attribute attribute in property.Attributes)
{ ... }
....
}
Run Code Online (Sandbox Code Playgroud)
集合属性.属性只包含"ConditionAttribute"类型的一个属性:带有"Test1"的属性.其他人被忽略;-(
那么AllowMultiple不适用于属性属性吗?
提前致谢
亨里克
我想使用ComponentModel DataAnnotations验证两个属性中至少有一个属性值.我的模型看起来像这样:
public class FooModel {
public string Bar1 { get; set; }
public int Bar2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想验证FooModel,以便需要Bar1 或 Bar2.换句话说,您可以输入一个,或另一个,或两者,但您不能将它们都留空.
我希望这对于服务器端和不显眼的客户端验证都有效.
编辑:这可能是重复,因为这看起来类似于我想要做的
基本上我想弄清楚的是如何要求在视图中填写两个字段中的至少一个.
在我的视图中,我有两个名为ISBN和ISBN13的文本字段.用户填写哪一个并不重要,只要其中一个填写.
我不确定在这里做什么期望考虑编写一个自定义验证器,所以我想我先问.我会包含一些代码,但由于它只是两个简单的字段,我认为这种解释会更好.
嗨,我已经找到了这个答案: MVC3验证 - 需要一个来自组
这对于检查组名并使用反射非常具体.
我的例子可能有点简单,我只是想知道是否有更简单的方法来做到这一点.
我有以下内容:
public class TimeInMinutesViewModel {
private const short MINUTES_OR_SECONDS_MULTIPLIER = 60;
//public string Label { get; set; }
[Range(0,24, ErrorMessage = "Hours should be from 0 to 24")]
public short Hours { get; set; }
[Range(0,59, ErrorMessage = "Minutes should be from 0 to 59")]
public short Minutes { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public short TimeInMinutes() {
// total minutes should not be negative
if (Hours <= 0 …Run Code Online (Sandbox Code Playgroud)