如何使用实体框架和ASP.Net MVC 2使用多个显示名称属性 [英] How to use multiple DisplayName attribute using Entity Framework and ASP.Net Mvc 2
问题描述
不同的地方我用我的课,我希望能够表现出不同的显示名称。结果
我有以下类:
[MetadataType(typeof运算(PortalMetaData))]
[System.Web.Mvc.Bind(不包括=PortalId)]
公共部分类门户网站
{
公共门户网站()
{
this.Created = DateTime.Now;
}
}
公共类PortalMetaData
{
[必需的(=的ErrorMessage门户名称是必需)]
[StringLength(50的ErrorMessage =门户名称必须在50个字符)]
公共对象PortalName {搞定;组; } [必需(的ErrorMessage =说明要求)]
公共对象描述{搞定;组; }
}
我在门户网站数据库中的相应表
我使用Portal表与网站管理员在门户网站更新表中记录的PortalController。
我想用不同的角色(AsstAdmin)能够更新此表以及其他用户。结果
为了方便,我想创建某种方式链接回到门户模式的单独部分类的。这将使我的AsstAdmin以显示更新的领域有限,我可以显示战场上的一个不同的名称为好。
如何完成这个任务?
如果我添加下面的类从门户继承比我得到一个异常:
无法投类型的对象
Project1.Mvc.Models.Portal到类型
Prpject1.Mvc.Models.Site。
块引用>[MetadataType(typeof运算(SiteMetaData))]
公共类网站:门户网站
{
公共网站(){}
}公共类SiteMetaData
{
[必填(显示名称=网站描述)]
公共对象描述{搞定;组; }
}
解决方案您可以创建一个具有唯一的字段每种类型的用户可以看到两种不同的视图模型。您将需要一个服务保存时做相应的映射回门户网站的实体。
Depending on where I use my Class, I want to be able to show a different DisplayName.
I have the following class:[MetadataType(typeof(PortalMetaData))] [System.Web.Mvc.Bind(Exclude = "PortalId")] public partial class Portal { public Portal() { this.Created = DateTime.Now; } } public class PortalMetaData { [Required(ErrorMessage = "Portal name is required")] [StringLength(50, ErrorMessage = "Portal name must be under 50 characters")] public object PortalName { get; set; } [Required(ErrorMessage = "Description is required")] public object Description { get; set; } }
I have a corresponding Table in the database Portal
I use the Portal table with a PortalController for the Site Admin to update the records in the Portal Table.
I want another user with a different Role (AsstAdmin) to be able to update this table as well.
To facilitate that I am thinking of creating a separate partial class that somehow links back to the Portal Model. This would allow me to display limited Fields for update by the AsstAdmin and I can display a different name for the Field as well.How can I accomplish this task? If I add the following class which inherits from Portal than I get an exception:
Unable to cast object of type 'Project1.Mvc.Models.Portal' to type 'Prpject1.Mvc.Models.Site'.
[MetadataType(typeof(SiteMetaData))] public class Site : Portal { public Site() { } } public class SiteMetaData { [Required(DisplayName = "Site Description")] public object Description { get; set; } }
解决方案You could create two different view models that have the only the fields each type of user can see. You will need a service to do the appropriate mapping back to the Portal entity when saving.
这篇关于如何使用实体框架和ASP.Net MVC 2使用多个显示名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!