在 Razor 视图中使用 System.Data.Linq [英] Using System.Data.Linq in a Razor view

查看:30
本文介绍了在 Razor 视图中使用 System.Data.Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能对这里发生的事情有一个根本的误解,但我在我的剃刀视图中循环遍历 LinqToSQL 类时遇到了问题:

I may have a fundamental misunderstanding of what is going on here, but I'm having an issue looping through a LinqToSQL class in my razor view:

<h3>Owners</h3>
@foreach (var ThisOwner in Prop.PropertyOwnerships.Where(p=p.bIsOwner.Value==true))
{
<div class="ODEditEntry">
...

我收到以下错误:

编译器错误消息:CS0012:在未引用的程序集中定义了类型System.Data.Linq.EntitySet`1".您必须添加对程序集System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"的引用.

Compiler Error Message: CS0012: The type 'System.Data.Linq.EntitySet`1' 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'.

我尝试将 @using System.Data.Linq 放在 cshtml 文件的顶部,但它告诉我 System.Data 命名空间中不存在 Linq.这显然不是真的,是的,我的项目中确实有 system.data.linq 作为参考.

I tried putting @using System.Data.Linq at the top of the cshtml file but it is telling me that Linq doesn't exist in the System.Data namespace. This is obviously not true and, yes, I do have system.data.linq as a reference in my project.

这里有什么想法吗?需要进口吗?我可以不在我的剃刀视图中做 Linq 风格的东西吗?那看起来……很奇怪?

Any Ideas here? Is a import needed? Can I just not do Linq style stuff in my razor views? That would seem....odd?

推荐答案

您需要通过在视图顶部添加 @using System.Data.Linq 来将命名空间导入到您的视图中.但是,如果您希望在所有视图中使用它,则需要将 <add namespace="System.Data.Linq"/> 添加到您的 Views 文件夹中的 web.config:

You need to import the namespace into your view by adding @using System.Data.Linq at the top of your view. However if you want it in all your views then you need to add <add namespace="System.Data.Linq" /> to the web.config in your Views folder:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Data.Linq" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

尽管与您的问题无关,但您确实应该尝试将此逻辑移出视图并移至控制器中,但这将使调试变得更加容易,并且意味着您的演示文稿与您的业务逻辑分离.

Although not relevent to your question you should really try to move this logic out of the view and into the controller, it will make things much easier to debug and means that your presentation is separated from your business logic.

这篇关于在 Razor 视图中使用 System.Data.Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆