在不同的命名空间中使用基类的属性和元数据

时间:2015-07-14 20:46:03

标签: c# asp.net-mvc entity-framework

我有一个单独的visual studio项目,我保留了我的数据模型(EF6)。因此,我的实体位于命名空间Name1(由EF6数据库首先创建,但在下面简化,对于此示例):

namespace Name1 {

   public class Person
   {
      public string FName {get; set;}
      public string LName {get; set;}
   }

}

现在,我已经创建了一个新的mvc 5项目,并引用了数据Visual Studio项目,以便我可以访问这些实体。在我的mvc5项目中,我想向实体添加一些元数据,如下所示:

namespace NameMvc 
{

   [MetadataType(typeof(PersonMetaData))]
   public class Person : Name1.Person
   {

   }

   public class PersonMetaData
   {
      [Display(Name = "Firstname")]
      public string FName;
   }
}

在我的控制器中,我希望得到所有人,所以我有这样的行动:

using Name1;
using NameMvc;

-- controller class code
public ActionResult Index()
{
   var persons = db.Person.ToList();
   return View(persons);
}
-- controller class code

在我看来,我尝试通过以下方式访问:

@model IEnumerable<NameMvc.Person>

现在,当我运行代码时出现错误:

传递到字典中的模型项的类型为&#39; System.Data.Entity.Infrastructure.DbQuery 1[Name1.Person]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [Name.MvcPerson]&#39;。 我可能在操作中做错了,因为db.Person来自Name1命名空间。 我想要的是在我的视图中使用元数据,这样,当我做这样的事情时:

@Html.DisplayNameFor(model => model.FName)

它将导致名字

0 个答案:

没有答案