在asp.net MVC 3中,有一个新属性允许我们将其他元数据传递给我们的视图,例如
[Required]
[AdditionalMetadata("Tooltip", "The title of the item")]
public string Title { get; set; }
Run Code Online (Sandbox Code Playgroud)
问题是,我如何在我的视图中实际使用这些信息?我认为也许它会将数据呈现为html 5数据属性,但事实并非如此.
一个简单的例子将非常感激.
我只是想知道为什么Range验证属性可以将Type和两个字符串作为参数?这是用于验证字符串对Enum或类似的东西?
我正在尝试做的是找到一种简单的方法来验证一个3字符串,它必须存在于枚举,任何sugestions?
谢谢,亚历克斯.
我有一个使用Entity Framework 4.1的关系,我的外键是自动生成的,外键的名称有下划线:
public class Setor : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
public virtual Secretaria Secretaria { get; set; }
}
public class Secretaria : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这在表Setor中生成了名为:Secretaria_Id的外键
我想删除这个下划线:SecretariaId
有没有办法做到这一点?我更喜欢使用DataAnnotations.
是否有可能增加更多Data Anootation成员,例如Range,Required,...到Entity Framework或Linq to SQL自动生成的类?
我想为我的类使用数据注释验证
谢谢
编辑1)
我为Northwind数据库创建一个实体框架模型并添加Product类.代码的一部分是这样的:
[EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new Product object.
/// </summary>
/// <param name="productID">Initial value of the ProductID property.</param>
/// <param name="productName">Initial value of the ProductName property.</param>
/// <param name="discontinued">Initial value of the Discontinued property.</param>
public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
{
Product …Run Code Online (Sandbox Code Playgroud) 我有不同的人应该看到不同名称的字段.
例如,假设我有以下用户类型:
public enum UserType {Expert, Normal, Guest}
Run Code Online (Sandbox Code Playgroud)
我实现了一个IMetadataAware属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class DisplayForUserTypeAttribute : Attribute, IMetadataAware
{
private readonly UserType _userType;
public DisplayForUserTypeAttribute(UserType userType)
{
_userType = userType;
}
public string Name { get; set; }
public void OnMetadataCreated(ModelMetadata metadata)
{
if (CurrentContext.UserType != _userType)
return;
metadata.DisplayName = Name;
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我可以根据需要覆盖其他值,但是当我不这样做时,可以使用默认值.例如:
public class Model
{
[Display(Name = "Age")]
[DisplayForUserType(UserType.Guest, Name = "Age (in years, round down)")]
public string Age { get; set; }
[Display(Name …Run Code Online (Sandbox Code Playgroud) 虽然我们主要使用流畅的配置来实现代码优先的POCO,但我们发现对表名,PK等使用数据注释很有用,因为它使得没有引用的非EF组件更容易使用到ObjectContext与这些实体进行交互.
根据我们的经验,似乎两种配置样式可以自由混合,流畅的配置覆盖DataAnnotations.这记录在哪里?这种混合配置有任何风险吗?
我们目前正在使用EF 4.3.1
c# entity-framework data-annotations ef-code-first entity-framework-4.3
我有一个电话号码通知模型(用户通过与他们的帐户关联的电话号码获得紧急情况通知,并且他们可以设置调用这些电话的顺序)。大多数情况下,模型的电话号码部分是必需的,但在创建新用户的过程中存在一种特殊情况,我们不想强制使用它。
我创建了一个非常简单的子对象 UserCreationPhoneNotificationModel,它继承自上述 UserPhoneNotificationModel。还有一些其他小的更改,但这里的相关更改是覆盖 PhoneNumber 字段,以便不再需要它。
在父模型中它是
[Required]
public virtual string PhoneNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
在子模型中它只是
public override string PhoneNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
我认为这可以解决问题,但显然不是。我认为问题在于 RequiredAttribute 上会有 Inherited = true ,但事实并非如此,所以我不完全确定为什么它会被继承到子类中。
我做了双重检查以确保,从父类中的字段中删除 Required 也使得子类中的字段不需要,所以这绝对是某种继承的东西。
我有一个视图模型,我从基类视图模型继承.我试图更改继承类中的DisplayName元数据,但它不起作用.
以下是视图模型:
namespace ViewModels
{
public class BaseViewModel
{
[DisplayName(Name = "Base Description")]
public virtual string Description { get; set; }
}
public class DerivedViewModel : BaseViewModel
{
[DisplayName(Name = "Derived Description")]
public override string Description { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
和控制器:
public ViewResult Create()
{
DerivedViewModel model = new DerivedViewModel();
model.Active = true;
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
呈现视图时,预期的显示名称是"派生描述",而是我得到"基本描述".
使用:MVC 5.1,.NET Framework 4.5,Visual Studio 2013
任何人都可以告诉我如何覆盖派生类中的显示数据注释?
假设我有一个这样的模型属性:
[Range(1, 31, ErrorMessage = "O dia de fechamento deve possuir valores entre 1 e 31")]
public int DataInicial { get; set; }
Run Code Online (Sandbox Code Playgroud)
即使在注释上设置了自定义错误消息,我仍然收到范围注释的默认错误消息“请输入小于或等于 31 的值。”,当我在@Html.TextBoxFor(model => model.DataInicial)场地。
我知道这篇文章,但我认为如果您可以在注释级别设置自定义消息,它应该可以在不设置 App_GlobalResources 和 .resx 文件、设置第三方库或其他任何内容的情况下工作......我知道添加 . resx 文件并将所有这些验证字符串放在那里,是一种“最佳实践”,但是......
那么,我可能哪里出错了,因为消息显示不正确?
先感谢您。
c# asp.net-mvc rangevalidator data-annotations asp.net-mvc-4
更新建议后,我仍然收到错误
和.SingleOrDefault()或试过FirstOrDefault()

我需要检索StringLength注释值,这是我的代码,但我收到以下错误.
我试图从这里实现几乎相同的代码,但得到错误:
序列不包含任何元素
public static class DataAnnotation
{
public static int GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName)
{
int maxLength = 0;
var attribute = modelClass.GetProperties()
.Where(p => p.Name == propertyName)
.Single()
.GetCustomAttributes(typeof(StringLengthAttribute), true)
.Single() as StringLengthAttribute;
if (attribute != null)
maxLength = attribute.MaximumLength;
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
//调用:
int length = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(EmployeeViewModel), "Name");
public class EmployeeViewModel
{
[StringLength(20, ErrorMessage = "Name cannot be longer than 20 characters.")]
public string Name{ get; set; …Run Code Online (Sandbox Code Playgroud)