出于某种原因,当我试图反思这个类时,.NET Reflector会引发异常.它适用于其他一切.
请问是什么源代码DataAnnotationsModelMetadataProvider?
modelmetadata data-annotations modelmetadataprovider asp.net-mvc-3 asp.net-mvc-2
更新:当我执行单元测试项目然后它将返回未处理此测试结果还包含内部异常而不是 "Assert.IsTrue失败.描述字段是必需的." 结果如(0 Pass,1 FAIL,1 Total)但是如果我使用F11进行调试,我们根本没有得到任何异常
[TestMethod]
[Asynchronous]
[Description("Determines whether the selected or single property is valide using the validation context or validate single properties.")]
public void ValidateSigleWithDataAnnotation()
{
LookupsServices lookupsservices = new LookupsServices();
Lookups lookups = new Lookups() { Description = "", LookupReference = 2, DisplayOrder = 50};
lookupsservices.Lookups.Add(lookups);
//THIS IS NOT WORKING
string message = ValidateProperties.ValidateSingle(lookups, "Description");
Assert.IsTrue(message.Equals(""), message);
//THIS IS WORKING
var results = new List<ValidationResult>();
Validator.TryValidateProperty(lookups.Description , new ValidationContext(lookups, null, null) { MemberName …Run Code Online (Sandbox Code Playgroud) 我收到The value 'abc' is not valid for fieldName.错误消息.这是默认的错误消息,我想以更简单的方式覆盖它.
截至目前我所尝试的内容如下所示
[RegularExpression(@"^\d+$",ErrorMessage="enter numeric value")][Integer(ErrorMessageResourceType = typeof(appName.Resources.abc.Resource),
ErrorMessageResourceName = "error_numeric")][RegularExpression("([1-9][0-9]*)")]Range(1,int.max,ErrorMessage="enter numeric value")
但无法更改默认错误消息.
建议我做最简单的方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace blueddPES.ViewModels
{
public class ContactViewModel
{
[Integer(ErrorMessage="sdfdsf")]
public int? hp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)我如何从DataAnnotations中捕获验证?我在这里研究,但我不明白它是如何工作的
所以我跳了一些你可以启发我的
public class Person // Represents person data.
{
/// <summary>
/// Gets or sets the person's first name.
/// </summary>
/// <remarks>
/// Empty string or null are not allowed.
/// Allow minimum of 2 and up to 40 uppercase and lowercase.
/// </remarks>
[Required]
[RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]
public string FirstName{ get; set;}
/// <summary>
/// Gets or sets the person's last name.
/// </summary>
/// <remarks>
/// Empty string or null are not allowed.
/// </remarks>
[Required] …Run Code Online (Sandbox Code Playgroud) 我正在使用自定义验证比较用户名.我正在检查它是否与旧值相同,或者如果它通过正则表达式,它是可以接受的,但如果不是,则抛出错误.如果可能,我如何从viewmodel获取UserID?
[EmailValidation]
public string Username{ get; set; }
public int UserID { get; set; }
public class EmailValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
User user= User.getUserByID(UserID); //How to get UserID?
string username= value.ToString();
if (user.Username== username || IsValid(username))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Error");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些DateTime值的自定义模型,以及一个DataAnnotation用于比较这些值的自定义模型.
这是带有注释的属性:
[Required]
[DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
[Display(Name = "Start Date")]
public DateTime StartTime { get; set; }
[DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
[Display(Name = "End Date")]
[CompareTo(this.StartTime, CompareToAttribute.CompareOperator.GreaterThanEqual)]
public DateTime? EndTime { get; set; }
Run Code Online (Sandbox Code Playgroud)
CompareTo属性是有问题的属性.我收到一个错误:
Keyword 'this' is not available in the current context
Run Code Online (Sandbox Code Playgroud)
我试过只StartTime放在注释中没有运气.如何从同一个模型类传递属性值?
我有一个模型和一个领域......等等这是微不足道的.但是,我需要从我的视图中获取字段中的"描述"属性和您的"名称"属性.使用@Html ...可以使用"DisplayNameFor"返回,显而易见.但是,如何获得"描述"属性?没有像@ Html.DisplayDescription这样的助手?有可能得到这个吗?
[Column("abc")]
[Display(Description = "The Description", GroupName = "The Group", Name = "The Name")]
public string field { get; set; }
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.对不起英语不好.;)
您是否可以为模型创建自定义数据注释,可以在T4模板中读取类似于View的属性.是否读取了文件夹?我想添加数据注释参数,比如我将构建视图的Scaffold.
谢谢
我正在使用Entity Framework并触发属性更改事件,如果更改的属性已映射,我想在其中更新属性。
protected void FooPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var notMappedArray = typeof(Foo).GetProperty(e.PropertyName).GetCustomAttributes(typeof(NotMappedAttribute), false); // I thought this might return null if the property did not have the attribute. It does not.
//if (notMappedArray == null)
UnitOfWork.Value.GetRepository<Foo>().Update(MyFoo);
}
Run Code Online (Sandbox Code Playgroud)
查找发送给此事件的属性是否在实体框架中映射的最佳方法是什么?
编辑:我已经看到了这个问题。但是,答案似乎有点过头了,还不能完全满足我的需要。
我正在使用带有EntityFramework 6 DataAnnotations的asp.net MVC 5。我想知道是否有一种方法来获取所有DisplayName对象并将它们保存在变量中到Controller类中。
例如,考虑以下类:
public class Class1
{
[DisplayName("The ID number")]
public int Id { get; set; }
[DisplayName("My Value")]
public int Value { get; set; }
[DisplayName("Label name to display")]
public string Label { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何获取DisplayName所有属性的值?例如,如何创建一个返回a的函数,该函数Dictionary< string,string >的键具有属性名称和值为的键DisplayName,如下所示:
{ "Id": "The ID name", "Value": "My Value", "Label": "Label name to display"}.
Run Code Online (Sandbox Code Playgroud)
我已经看到了这个主题stackoverflow-获取DisplayName属性的值,但是我不知道扩展此代码的方法。
data-annotations ×10
c# ×5
asp.net-mvc ×4
validation ×4
mvvm ×2
asp.net ×1
attributes ×1
model ×1
properties ×1
t4 ×1