简体   繁体   English

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

[英]use properties and metadata on base class in different namespace

I have a separate Visual Studio project where I keep my data model (EF6).我有一个单独的 Visual Studio 项目,用于保存我的数据模型 (EF6)。 So, my entities are in namespace Name1 (created by EF6 database first, but simplified below, for this example):因此,我的实体位于命名空间Name1 (首先由 EF6 数据库创建,但在此示例中简化如下):

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

Now, I have created a new MVC 5 project, which references the data Visual Studio project so that I can access the entities.现在,我创建了一个新的 MVC 5 项目,它引用了数据 Visual Studio 项目,以便我可以访问实体。 In my MVC project, I want to add some metadata to the entity like this:在我的 MVC 项目中,我想向实体添加一些元数据,如下所示:

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

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

In my controller I want to get all the persons, so I have an Action like this:在我的控制器中,我想得到所有人,所以我有一个这样的动作:

using Name1;
using NameMvc;

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

And in my view I try to access that via:在我看来,我尝试通过以下方式访问它:

@model IEnumerable<NameMvc.Person>

Now, when I run the code I get an error:现在,当我运行代码时,出现错误:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Name1.Person]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Name.MvcPerson]'.传递到字典中的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[Name1.Person]”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Name”的模型项.MvcPerson]'。

I probably did something wrong in the action, because db.Person comes from the Name1 namespace.我可能做的动作有些不对劲,因为db.Person来自Name1命名空间。

I want to use the metadata in my View so that when I do something like this @Html.DisplayNameFor(model => model.FName) , it displays "Firstname".我想在我的视图中使用元数据,这样当我做这样的事情@Html.DisplayNameFor(model => model.FName) ,它会显示“名字”。

You are getting an error rendering your view because you are returning IEnumerable<Name1.Person> from your action, but your view is expecting IEnumerable<NameMvc.Person> .您在渲染视图时遇到错误,因为您从操作中返回IEnumerable<Name1.Person> ,但您的视图期待IEnumerable<NameMvc.Person> When you are using a strongly-typed model in your view, it must match the model returned from the action.当您在视图中使用强类型模型时,它必须与操作返回的模型相匹配。

There are two possible solutions for this error:此错误有两种可能的解决方案:

  1. Change your view to use IEnumerable<Name1.Person> as its model, or更改您的视图以使用IEnumerable<Name1.Person>作为其模型,或
  2. Change your action to return an IEnumerable<NameMvc.Person> as the model.更改您的操作以返回IEnumerable<NameMvc.Person>作为模型。

When you use MetadataType to add metadata to your model, the most common way is to use the fact that the original model class is generated as a partial class.当您使用MetadataType向模型添加元数据时,最常见的方法是利用原始模型类作为分部类生成的事实。 You create another "part" to the partial class and add the MetadataType attribute to it.您为分部类创建另一个“部分”并向其添加MetadataType属性。

However, partial classes cannot be used across assemblies.但是,分部类不能跨程序集使用。 So this means that if you move your model to its own assembly, then you cannot add a partial class to your MVC project to add the metadata.因此,这意味着如果您将模型移至其自己的程序集,则无法向 MVC 项目添加分部类以添加元数据。

To solve this, you can do one of the following:要解决此问题,您可以执行以下操作之一:

  1. Add your metadata in your model's assembly.在模型的程序集中添加元数据。 To do this, you use the partial class solution in your model's assembly.为此,您在模型的程序集中使用分部类解决方案。 The problem with this is that you're now mixing view logic with data logic.问题在于您现在将视图逻辑与数据逻辑混合在一起。
  2. Create a new class in your MVC project which is a pseudo-copy of your data model to act as a view model.在您的 MVC 项目中创建一个新类,它是您的数据模型的伪副本,用作视图模型。 You add your metadata to that.您将元数据添加到其中。 Your action will return this view model and your view will use that.您的操作将返回此视图模型,您的视图将使用它。 In your action, you copy the data from your data model to your view model.在您的操作中,您将数据从数据模型复制到视图模型。

I prefer option #2 for a couple of reasons:我更喜欢选项#2 有几个原因:

  1. It solves the problem you're facing, and它解决了您面临的问题,并且
  2. How data is presented to the user is often different than how I want it represented in my databases.数据呈现给用户的方式通常与我希望它在我的数据库中呈现的方式不同。 This mapping allows me to handle that nicely.这个映射让我可以很好地处理这个问题。

The drawback to option #2 is the repetitive copying of data.选项#2 的缺点是重复复制数据。 However you could use tools like AutoMapper to simplify the data copying.但是,您可以使用AutoMapper 之类的工具来简化数据复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM