ASP.NET MVC Razor 视图,未正确显示 Linq to SQL 相关实体

发布于 2024-12-02 09:02:09 字数 1080 浏览 0 评论 0原文

我在带有 Razor 视图的 ASP.NET MVC3 项目中使用 Linq to SQL。

我的 L2S 对象似乎在控制器/模型代码中运行良好;我可以引用和使用相关对象的子类集合。然而,在我的 Razor 观点中,我无法这样做。 Intellisense 可以识别子类集合:

Model.ChildObjects

但它无法为 ChildObjects 对象本身提供任何代码完成。尝试像这样将集合用作集合:

foreach(var item in Model.ChildObjects)

会导致编辑器中出现以下内联错误:

foreach statement cannot operate on variables of type
System.Data.Linq.EntitySet<ChildObjectType> because 
System.Data.Linq.EntitySet<ChildObjectType> does not
contain a public definition for 'GetEnumerator'

从 Razor 视图引用的内容似乎有问题。当我尝试使用这样的代码时:

@Model.ChildObjects.Count()

执行时出现此编译错误:

The type 'System.Data.Linq.EntitySet`1<T0>' is defined in an assembly that is
not referenced. You must add a reference to assembly 
'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

我没有更改项目的 web.config 文件中的任何内容。我最近将我的 Windows 7 更新到了 SP1,并更新了我的 NuGet...但我不确定其中是否会涉及到任何内容。

I'm using Linq to SQL in an ASP.NET MVC3 project with Razor views.

My L2S objects seem to work fine in controller/model code; I can reference and work with child class collections for my related objects. However, I am unable to do so in my Razor views. Intellisense recognizes the child class collection:

Model.ChildObjects

But it is unable to provide any code completion for the ChildObjects object itself. Trying to use the collection as a collection like so:

foreach(var item in Model.ChildObjects)

results in the following inline error in the editor:

foreach statement cannot operate on variables of type
System.Data.Linq.EntitySet<ChildObjectType> because 
System.Data.Linq.EntitySet<ChildObjectType> does not
contain a public definition for 'GetEnumerator'

There seems to be something wrong with what is being referenced from the Razor view. When I try to use code like this:

@Model.ChildObjects.Count()

I get this compilation error upon executing:

The type 'System.Data.Linq.EntitySet`1<T0>' is defined in an assembly that is
not referenced. You must add a reference to assembly 
'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I haven't changed anything in the web.config file(s) for the project. I have recently updated my Windows 7 to SP1, and updated my NuGet... but I'm not sure if there would be anything involved there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-12-09 09:02:09

我不知道这样做的原因,因此我会暂时保留这个问题,以防有人可以解释为什么我所做的事情有效;

我发现了这个问题:在 Razor 视图中使用 System.Data.Linq< /a>

其中,有人提到将对 System.Data.Linq 的引用设置为 'Copy Local' = true。这样做已经解决了我在这里遇到的问题。

I don't know the reason for this, and will therefore leave this question open for a bit in case someone can explain why what I did worked;

I found this question: Using System.Data.Linq in a Razor view

In it, someone mentions setting the reference to System.Data.Linq to 'Copy Local' = true. Doing that has solved the issue I am experiencing here.

在巴黎塔顶看东京樱花 2024-12-09 09:02:09

尝试在 views 文件夹的 web.config 中添加命名空间 System.Data.Linq

Try adding the namespace System.Data.Linq in the web.config in the views folder.

爱人如己 2024-12-09 09:02:09

尽管您似乎已经解决了您的问题,但我认为潜在的更大问题是您将域模型传递给您的视图。我建议不要这样做,因为它使您的视图文件特别依赖于您的数据层。

最好创建包含视图所需信息的视图模型。控制器将负责通过数据层选择数据并将其转换为视图模型,然后您可以将验证属性添加到视图模型中。这是一个更干净的设计。

Although you seemed to have solved your issue, I think the potentially bigger issue is that you are passing domain models to your view. I'd recommend against that, as it makes your view files specifically dependent on your data layer.

It would be better to create view models that contain the information the view requires. The controller would become responsible for selecting data through your data layer and transforming it into view models, which you can then add your validation attributes to. It's a much cleaner design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文