ASP.NET MVC Razor 视图,未正确显示 Linq to SQL 相关实体
我在带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道这样做的原因,因此我会暂时保留这个问题,以防有人可以解释为什么我所做的事情有效;
我发现了这个问题:在 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.尝试在
views
文件夹的 web.config 中添加命名空间System.Data.Linq
。Try adding the namespace
System.Data.Linq
in the web.config in theviews
folder.尽管您似乎已经解决了您的问题,但我认为潜在的更大问题是您将域模型传递给您的视图。我建议不要这样做,因为它使您的视图文件特别依赖于您的数据层。
最好创建包含视图所需信息的视图模型。控制器将负责通过数据层选择数据并将其转换为视图模型,然后您可以将验证属性添加到视图模型中。这是一个更干净的设计。
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.