asp.net-mvc – 依赖于另一个字段的属性
发布时间:2020-05-23 13:49:50 所属栏目:asp.Net 来源:互联网
导读:在我的ASP.NET MVC应用程序的模型中,我想要验证一个文本框,只有当选中了一个特定的复选框时才需要。 就像是 public bool retired {get, set};[RequiredIf(retired,true)]public string retirementAge {get, set}; 我怎样才能做到这一点? 谢谢。 看看这个:
|
在我的ASP.NET MVC应用程序的模型中,我想要验证一个文本框,只有当选中了一个特定的复选框时才需要。 就像是 public bool retired {get,set};
[RequiredIf("retired",true)]
public string retirementAge {get,set};
我怎样才能做到这一点? 谢谢。 解决方法看看这个: http://blogs.msdn.com/b/simonince/archive/2010/06/04/conditional-validation-in-mvc.aspx我已经修改了代码,以满足我的需要。也许你也受益于这些变化。 public class RequiredIfAttribute : ValidationAttribute
{
private RequiredAttribute innerAttribute = new RequiredAttribute();
public string DependentUpon { get; set; }
public object Value { get; set; }
public RequiredIfAttribute(string dependentUpon,object value)
{
this.DependentUpon = dependentUpon;
this.Value = value;
}
public RequiredIfAttribute(string dependentUpon)
{
this.DependentUpon = dependentUpon;
this.Value = null;
}
public override bool IsValid(object value)
{
return innerAttribute.IsValid(value);
}
}
public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>
{
public RequiredIfValidator(ModelMetadata metadata,ControllerContext context,RequiredIfAttribute attribute)
: base(metadata,context,attribute)
{ }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
// no client validation - I might well blog about this soon!
return base.GetClientValidationRules();
}
public override IEnumerable<ModelValidationResult> Validate(object container)
{
// get a reference to the property this validation depends upon
var field = Metadata.ContainerType.GetProperty(Attribute.DependentUpon);
if (field != null)
{
// get the value of the dependent property
var value = field.GetValue(container,null);
// compare the value against the target value
if ((value != null && Attribute.Value == null) || (value != null && value.Equals(Attribute.Value)))
{
// match => means we should try validating this field
if (!Attribute.IsValid(Metadata.Model))
// validation failed - return an error
yield return new ModelValidationResult { Message = ErrorMessage };
}
}
}
}
然后使用它: public DateTime? DeptDateTime { get; set; }
[RequiredIf("DeptDateTime")]
public string DeptAirline { get; set; } (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 如何正确处理n层应用程序中的错误?
- Asp.net MVC json还是Json.net?
- 如何使用ASP.NETC#从服务器端确定浏览器类型?
- 让OData和NHibernate结合进行动态查询
- asp.net-mvc – ASP.NET MVC:通过两个控制器操作重用视图
- asp.net-mvc – 支持RavenDB的MVC4的会员系统
- 如何动态地从ASP.NET MVC控制器获取数据到jQuery?
- asp.net – IItemTransform和现有的缩小文件
- asp.net – requestValidationMode 4.5 vs 2.0
- ASP.NET Core 2.0中Razor页面禁用防伪令牌验证
推荐文章
站长推荐
- 使用Asp.net核心将PDF返回到浏览器
- asp.net-mvc-routing – 在MVC 6控制器中使用url
- iis-6 – Asp.net mvc 4 on iis6:“无法找到该页
- asp.net – System.Linq.Dynamic不支持OrderByDe
- asp.net-mvc – Razor并在ActionLinks上指定css类
- asp.net – VS 2010失败调试:HttpException在Lo
- asp.net-core – 编译netcoreapp1.0,代码包含#if
- asp.net-mvc-4 – Quartz.NET触发器不会触发,MVC
- ASP.NET -- WebForm -- HttpResponse 类的方法和
- 阻止拦截ASP.NET Web API响应的FormsAuthenticat
热点阅读
