我在ASP.NET MVC 2站点中使用System.ComponentModel.DataAnnotations验证属性修饰了ViewModel .对于我的一个名为Price的字段,我想验证该值是否低于某种程度,在本例中为0.
我知道,RangeAttribute存在具有较低和上部程度的验证,但确实像一个MinimumValueAttribute存在的验证,只有较低的程度?
如果没有,我想我会自己动手.
c# asp.net-mvc asp.net-mvc-2-validation data-annotations asp.net-mvc-2
我有一个实现IValidatableObject的视图模型,并且还有几个验证属性.当我尝试在我的ApiController上调用一个动作时,只执行属性验证.ASP.NET Web API不支持IValidatableObject吗?复杂验证的替代方法是什么,不能用单个属性表示?
编辑:在某个地方,我必须搞砸了一些东西.验证神秘地开始按预期工作.看起来默认情况下肯定支持IValidatableObject.
validation data-annotations ivalidatableobject asp.net-web-api
我有一个StartDate和EndDate我的SchoolEvents模型,我想知道是否有任何数据注释我可以用来验证StartDate小于或等于,EndDate并且EndDate大于或等于StartDate?
我创建了以下自定义RegularExpressionAttribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" };
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel中的字段使用我的AlphaNumeric属性进行修饰:
[AlphaNumeric(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.DriverLicenseNumber_RegexError_)]
public string DriverLicenseNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
该字段构建在视图中:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.DriverLicenseNumber)
@Html.ValidationMessageFor(m => m.DriverLicenseNumber)
@Html.TextBoxFor(m => m.DriverLicenseNumber)
}
Run Code Online (Sandbox Code Playgroud)
这应该在我的html输入标记上产生适当的 …
data-annotations unobtrusive-validation asp.net-mvc-3 asp.net-mvc-4
我有ASP.NET MVC网页,我使用DataAnnotation来验证客户端的表单.其中一个视图类具有如下属性:
[StringLength(100, MinimumLength = 3, ErrorMessage = "Länken måste vara mellan 3 och 100 tecken lång")]
[Display(Name = "Länk")]
[RegularExpression(@"^(http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?)?$", ErrorMessage="Länkgen är inte giltlig")]
public string Url { get; set; }
Run Code Online (Sandbox Code Playgroud)
在视图中,我将此代码用于属性:
@Html.LabelFor(c => c.Url, true)
@Html.TextBoxFor(c => c.Url, new { @class = "tb1", @Style = "width:400px;" })
@Html.ValidationMessageFor(model => model.Url)
Run Code Online (Sandbox Code Playgroud)
粘贴到这样的URL时:
http://95rockfm.com/best-voicemail-giving-play-by-play-of-car-accident/
网页将锁定,我无法在wepage上做任何事情.如果我将其粘贴到:
http://95rockfm.com/best-voicemail-giving-play-by-play-of-car-accident
它工作得很好.
这些javascript文件包含在网页的底部:
<script type="text/javascript" src="/Scripts/jquery.qtip.min.js"></script>
<script src="/Scripts/jquery-1.7.1.min.js"></script>
<script src="/Scripts/jquery-ui-1.8.20.min.js"></script>
<script src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
完全相同的问题同时适用于IE和Chrome.然而,IE将回来并说一个脚本花了很长时间,一个按钮停止脚本.但是当切换输入控件时,脚本将再次运行并查看网页.
我没有使用任何自定义的东西,为什么我得到这个?
我有一个自定义的ValidationAttribute,它检查另一个用户是否已经存在。为此,它需要访问我的数据访问层,这是一个由Unity注入到我的控制器中的实例
如何将它(或与此有关的任何东西)作为参数传递给自定义验证器?
这可能吗?即我在哪里创建Dal,那应该是一个参数
public class EmailIsUnique : ValidationAttribute
{
private string _errorMessage = "An account with this {0} already exists";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DataAccessHelper Dal = new DataAccessHelper(SharedResolver.AppSettingsHelper().DbConnectionString); //todo, this is way too slow
bool isValid = true;
if(value == null) {
isValid = false;
_errorMessage = "{0} Cannot be empty";
} else {
string email = value.ToString();
if (Dal.User.FindByEmail(email) != null)
{
isValid = false;
}
}
if (isValid)
return ValidationResult.Success;
else
return new …Run Code Online (Sandbox Code Playgroud) 有谁知道如何访问枚举类型的"显示名称"数据注释?
我有一个带显示名称的枚举类型
class enum SomethingType {
[Display(Name = "Type 1")]
Type1,
[Display(Name = "Type 2")]
Type2
}
Run Code Online (Sandbox Code Playgroud)
以及引用它的模型类
class ModelClass {
public SomethingType Type {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何在ModelClass中显示值的显示名称?
谢谢.
我有一个类在类级别定义数据注释.元数据类具有与之关联的自定义属性,以及通常的DisplayName,DisplayFormat等.
public class BaseMetaData
{
[DisplayName("Id")]
public object Id { get; set; }
[DisplayName("Selected")]
[ExportItem(Exclude = true)]
public object Selected { get; set; }
}
[MetadataType(typeof(BaseMetaData))]
public class BaseViewModel
{
public int Id { get; set; }
public bool Selected { get; set; }
Run Code Online (Sandbox Code Playgroud)
给定类型T,如何从元数据类中检索自定义属性?下面的尝试不起作用,因为元数据属性来自BaseViewModel而不是BaseMetaData类.
需要一般工作,即不能做typeof(BaseMetaData).GetProperty(e.PropertyName).想知道是否有从类中获取MetadataType的方法,那么它将使其成为可能.
var type = typeof (T);
var metaData = ModelMetadataProviders.Current.GetMetadataForType(null, type);
var propertMetaData = metaData.Properties
.Where(e =>
{
var attribute = type.GetProperty(e.PropertyName)
.GetCustomAttributes(typeof(ExportItemAttribute), false)
.FirstOrDefault() as ExportItemAttribute; …Run Code Online (Sandbox Code Playgroud) Swift是否具有类似Java的注释(可自定义)来存储类或方法元数据?
@SuppressWarnings(value = "unchecked")
void maMethode() { }
Run Code Online (Sandbox Code Playgroud)
如果不是,它将来是否会支持它?
我有一个具有datetime属性的模型,我想确保在视图中,除非该编辑器具有值,否则无法提交表单.
employee {
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Required] // <- this isn't doing anything for me???
public DateTime DateOfBirth {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
有没有我可以使用的注释,或者我必须在页面中使用javascript?
还是有另一种解决方案?
更新 -
当我清除日期编辑器时,我在编辑框中得到以下信息:
mm/dd/yyyy
Run Code Online (Sandbox Code Playgroud)
当我提交这个,这算是空的还是什么?使DateTime属性可以为空可以解决我的问题,当我提交一个日期为mm/dd/yyyy的表单时,没有进行验证
data-annotations ×10
c# ×6
asp.net-mvc ×4
asp.net ×2
annotations ×1
datetime ×1
entity ×1
frameworks ×1
metadata ×1
reflection ×1
regex ×1
swift ×1
validation ×1