2012-09-25 49 views
1

我在使用Scaffolding Nuget创建CRUD视图的MVC 4项目中使用了Data Annotations。我正在使用层级数据库模型而不是EF。MVC 4数据注释

所以我的课的样子如下:

[MetadataType(typeof(CustomerMetaData))] 
public partial class UserProfile: IBrObject 
{ 
    public UserProfile(string aspUserName): this() 
     { 
      this.AspUserName = aspUserName; 
     } 

    public string AspUserName { get; set; } 
    public DateTime MetaDateFirstSaved { get; set; } 
} 

public class CustomerMetaData 
{ 
      [ReadOnly(true)]    
      [ScaffoldColumn(false)] 
      [DisplayName("ASP UserName")] 
      public object AspUserName { get; set; } 
      [DisplayName("Date First Saved")] 
      [DataType(DataType.Date)] 
      public object MetaDateFirstSaved { get; set; } 
} 

当我试图创建脚手架的NuGet意见仍然显示AspUserName列不隐藏或只有不读。 我如何隐藏或只读?

+1

可能重复[是否只读(真)与Html.EditorForModel合作?](http://stackoverflow.com/questions/3510081/does -readonlytrue-work-with-html-editorformodel) –

+0

“Mystere Man”uihint属性可能是正确的选择。谢谢 –

回答

1

您在元数据中使用object,在实际视图模型中分别使用stringDateTime,因此它们不匹配。

更新

另一种可能性(我很肯定这是什么在你的情况发生),是因为在视图模型类型定义为接口类型,而不是类类型。

在您的意见中,将@model IBrObject替换为@model UserProfile

希望这有助于

+0

我从下面的示例看http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx –

+0

我试过相同的数据类型没有对象,但没有运气。 –

+0

更新了我的答案。一旦问题得到解决,请不要忘记将答案标记为已接受。谢谢 –

0

对象AspUserName!=字符串AspUserName