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

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

问题描述

我可能有什么是怎么回事了根本性的误解,但我有通过LinqToSQL类在我的Razor视图循环的一个问题:

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">
...

我收到以下错误:

I'm getting the following error:

编译器错误信息:CS0012:类型'System.Data.Linq.EntitySet`1'在未引用的程序集定义。您必须添加到程序集的引用将System.Data.Linq,版本= 4.0.0.0,文化=中性公钥= 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'.

我试图把在CSHTML文件的顶部@using System.Data.Linq程序,但它告诉我,LINQ的不System.Data命名空间中存在。这显然​​是不正确的,是的,我确实有将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 在命名空间导入到您的看法您的视图的顶部。然而,如果你想在你所有的观点,那么你需要添加&LT;添加命名空间=System.Data.Linq程序/&GT; ,以便在视图中的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>

虽然没有相应和你的问题,你真的应该尝试移动这个逻辑了看法,并到控制器,它会使事情更易于调试,意思是你的presentation从业务逻辑分离。

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天全站免登陆