ASP.NET MVC/LINQ:在视图中迭代 Linq.EntitySet 的正确方法是什么?

发布于 2024-08-30 05:07:21 字数 1396 浏览 8 评论 0原文

好的,我有一个强类型的客户“详细信息”视图,它采用客户对象模型。

我正在使用 LINQ to SQL,每个客户都可以拥有多个(停车位)。

这是数据库中的 FK 关系,因此我的 LINQ 生成的 Customer 模型有一个“Spaces”集合。伟大的!

这是来自我的 CustomerRepository 的代码片段,我在其中迭代客户的停车位以删除所有付款、车位,最后删除客户:

public void Delete(Customer customer)
{
    foreach (Space s in customer.Spaces)
        db.Payments.DeleteAllOnSubmit(s.Payments);
    db.Spaces.DeleteAllOnSubmit(customer.Spaces);
    db.Customers.DeleteOnSubmit(customer);
}

一切都按预期进行!

现在,在我的“详细信息”视图中,我想用客户空间填充表:

<% foreach (var s in Model.Spaces)
   { %>
    <tr>
        <td><%: s.ID %></td>
        <td><%: s.InstallDate %></td>
        <td><%: s.SpaceType %></td>
        <td><%: s.Meter %></td>
    </tr>
<% } %>

我收到以下错误:

foreach 语句无法对“System.Data.Linq.EntitySet”类型的变量进行操作,因为“System.Data.Linq.EntitySet”不包含“GetEnumerator”的公共定义

最后,如果我将这段代码添加到我的 Customer 部分类并使用视图中的 foreach 来迭代 ParkingSpaces 一切都按预期工作:

public IEnumerable<Space> ParkingSpaces
{
    get
    {
        return Spaces.AsEnumerable();
    }
}

这里的问题是我不想重复自己。我还认为我可以使用 ViewModel 将 Spaces 集合传递给 View,但是 LINQ 已经在 Customer 模型上推断并创建了 Spaces 属性,因此我认为仅使用它是最干净的。

我错过了一些简单的事情,或者我处理这个问题的方法不正确?

谢谢!

OK so I have a strongly-typed Customer "Details" view that takes a Customer object Model.

I am using LINQ to SQL and every Customer can have multiple (parking) Spaces.

This is a FK relationship in the database so my LINQ-generated Customer model has a "Spaces" collection. Great!

Here is a code snippet from my CustomerRepository where I iterate through the Customer's parking spaces to delete all payments, spaces and then finally the customer:

public void Delete(Customer customer)
{
    foreach (Space s in customer.Spaces)
        db.Payments.DeleteAllOnSubmit(s.Payments);
    db.Spaces.DeleteAllOnSubmit(customer.Spaces);
    db.Customers.DeleteOnSubmit(customer);
}

Everything works as expected!

Now in my "Details" view I want to populate a table with the Customer's Spaces:

<% foreach (var s in Model.Spaces)
   { %>
    <tr>
        <td><%: s.ID %></td>
        <td><%: s.InstallDate %></td>
        <td><%: s.SpaceType %></td>
        <td><%: s.Meter %></td>
    </tr>
<% } %>

I get the following error:

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

Finally, if I add this bit of code to my Customer partial class and use the foreach in the view to iterate through ParkingSpaces everything works as expected:

public IEnumerable<Space> ParkingSpaces
{
    get
    {
        return Spaces.AsEnumerable();
    }
}

The problem here is that I don't want to repeat myself. I was also thinking that I could use a ViewModel to pass a Spaces collection to the View, however LINQ already infers and creates the Spaces property on the Customer model so I think it would be cleanest to just use that.

I am missing something simple or am I approaching this incorrectly?

Thanks!

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

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

发布评论

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

评论(4

蒗幽 2024-09-06 05:07:21

抱歉,如果我回答这个问题有点晚了,但解决问题的正确方法是添加对 web.config 文件的程序集引用,以便视图可以访问 GetEnumerator() 方法。您可以使用页面编译错误中提供的程序集引用字符串来执行此操作。
例如

Sorry if Im a bit late answering this but the correct method of solving your problem is to add an assembly reference to your web.config file so that the view can access the GetEnumerator() method. You can do this using the assembly reference string provided in the page compliation error.
eg

<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

帝王念 2024-09-06 05:07:21

除了小助手方法之外,还有两种方法可以做到这一点。

您可以在页面中继承类的 IEnumerable:

<%@ Page Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Space>>" %>

<% foreach (var item in Model)
   {

或者您可以将实体对象强制转换为 IEnumerable:

<% foreach (var item in Model.Spaces as IEnumerable<Space>)
   {

There are two ways you can do it, other than your little helper method.

You can inherit an IEnumerable of your class in the page:

<%@ Page Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Space>>" %>

<% foreach (var item in Model)
   {

Or you can cast your entity object to an IEnumerable:

<% foreach (var item in Model.Spaces as IEnumerable<Space>)
   {
夏末 2024-09-06 05:07:21
  • 首先使用视图模型
    而不是直接访问 DTO
    首选。 (为此使用 Automapper

  • 其次,强烈输入您的观点
    View Model 和 View Model 中有
    传递给的 IEnumerable 或 List
    view,然后你可以迭代它

  • First of all using View Models
    instead of directly accessing DTOs is
    prefered. (use Automapper for this)

  • Second, Strongly type your views to
    View Model and in the View Model have
    an IEnumerable or List passed to the
    view, then you can iterate through it

北方的韩爷 2024-09-06 05:07:21

只需将对 System.Data.Linq 的引用添加到您的项目即可。

Just add a reference to System.Data.Linq to your project.

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