【问题标题】:Does not contain a definition for 'AsEnumerable' and 'cannot convert from methodgroup' in Razor viewRazor 视图中不包含“AsEnumerable”和“无法从方法组转换”的定义
【发布时间】:2016-11-22 00:15:02
【问题描述】:

我遇到了一个我找不到解决方案的错误,这是我的模型不包含“AsEnumerable”的定义(我将整个错误放在帖子的下方),我已经阅读过它在 Stack Overflow 周围,所有答案似乎都指向 using system.Linqusing system.Data,但我在代码中有它们。

该程序应该从表中获取一些数据:Contatti,Contatti,Contacts和Company,它们位于不同的位置并将它们放在一起(发生在控制器中的GET方法列表中),代码如下:

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

 public partial class ContactsUni2
{
    public IEnumerable<Contatti> Contattis { get; set; }
    public IEnumerable<Companies> Companies { get; set; }
    public IEnumerable<Contact> Contacts { get; set; }
}

控制器 GET 给出错误:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ContactManager.Models;

namespace ContactManager.Controllers
{
public class ContactsUni21Controller : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();
    private ContattiDB2 db2 = new ContattiDB2();

    // GET: ContactsUni21
    public ActionResult Index(String Page)
    {
        ContactsUni2 CU = new ContactsUni2();
        CU.Contattis = db.Contattis.Include(i => i.ContattoID);
        CU.Contacts = db.Contacts;
        CU.Companies = db.Companies;
        List<ContactsUni2> contactlist = new List<ContactsUni2>();
        contactlist.Add(CU.AsEnumerable()); // The error is HERE at CU.AsEnumerable(), cannot convert from method group to 'contactsuni2'
        return View(contactlist);
    }

查看,这也给出了类似的错误,但对于 Contatti.Count()

@model IEnumerable<ContactManager.ViewModels.ContactsUni3>

@{
ViewBag.Title = "Index";
}

<h2>Contacts Unified</h2>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Contatti.Nome)
            @Html.DisplayNameFor(model => model.Contatti.Citta)
            @Html.DisplayNameFor(model => model.Contatti.ContattoID)
            @Html.DisplayNameFor(model => model.Contatti.CodicePostale)
            @Html.DisplayNameFor(model => model.Contatti.Email)
            @Html.DisplayNameFor(model => model.Contact.Address)
            @Html.DisplayNameFor(model => model.Contact.CompanyId)
            @Html.DisplayNameFor(model => model.Contact.ContactId)
            @Html.DisplayNameFor(model => model.Company.CompanyName)

        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        for (int i = 0; i < @item.Contatti.Count(); i++) // Second Error Here
        {
            <tr>
                <td>
                    @item.Contatti[i].Nome
                    @item.Contatti[i].Citta
                    @item.Contatti[i].ContattoID
                    @item.Contatti[i].CodicePostale
                    @item.Contatti[i].Email
                    @item.Contatti[i].Address
                    @item.Contatti[i].CompanyId
                    @item.Contatti[i].ContactId
                    @item.Contatti[i].CompanyName

                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ContattoID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.ContattoID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ContattoID })
                </td>
            </tr>
        }
    }

</table>
</body>
</html>

Controller GET 方法中的第一个错误:

错误 CS1929“ContactsUni2”不包含“AsEnumerable”的定义,并且最佳扩展方法重载“DataTableExtensions.AsEnumerable(DataTable)”需要“DataTable”类型的接收器

当我尝试这样做时,它会说:

无法从方法组转换为“contactsuni2”

视图循环中的第二个错误:

错误 CS1061“Contatti”不包含“Count”的定义,并且找不到接受“Contatti”类型的第一个参数的扩展方法“Count”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • 您在代码中拥有这些命名空间,但在您的视图中却没有。在您的视图或视图的配置中添加@using System.Linq
  • 你不是说CU.Contacts.AsEnumerable()

标签: c# asp.net asp.net-mvc


【解决方案1】:

就用这个吧:

contactlist.Add(CU); 

List.Add() 方法需要单个项目,而 CU 是单个项目。

要解决视图中的问题:只需在顶部添加一行带有@using System.Linq 的行,这样编译器就会知道在哪里查找。


关于 AsEnumerable 的其他信息:

如果您有一个集合或项目序列,则只需要AsEnumerable()但是该集合与 foreach (var item in collection) 等构造不兼容。
在这种情况下,您可以尝试使用foreach (var item in collection.AsEnumerable())

在某些情况下,.NET Framework 可能已经提供了.AsEnumerable() 的实现,在其他情况下,您可能必须自己提供。但就您的问题而言,您甚至根本不需要它。

【讨论】:

  • 谢谢,这解决了第一个问题,第二个虽然@using System.Linq 没有帮助,但我仍然无法遍历它,它说不能将 [] 索引应用于类型表达式联系人/联系人/公司。我需要从每个表中获取特定的列。
  • 您的收藏定义为public IEnumerable&lt;Contatti&gt; Contattis。而IEnumerable&lt;...&gt; 不支持使用[i] 进行索引,它只支持使用例如枚举。 foreach。您可以通过将其更改为:public List&lt;Contatti&gt; Contattis 来解决此问题。这可能会导致需要进行其他更改,例如分配它时,只需添加.ToList() 如果/当发生这种情况。
【解决方案2】:

您有 2 个不同的问题:

  1. 在您的 ContactsUni21Controller 中,您正在对未实现 IEnumerable 的对象调用 AsEnumerable;您正在尝试将 ContactsUni2 的实例添加到 List&lt;ContactsUni2&gt; 此处不需要强制转换,因为它是列表包含的同一类型的实例

  2. 正如@CodeCaster 所述,您没有在 Razor 视图中放置 @using System.Linq; 语句,从而阻止您访问 IEnumerable.Count 方法

【讨论】:

  • 谢谢,这解决了第一个问题,第二个虽然@using System.Linq 没有帮助,但我仍然无法遍历它,它说不能将 [] 索引应用于类型表达式联系人/联系人/公司。我需要从每个表中获取特定的列。
  • IEnumerable 不支持索引和获取;您需要调用ToList 然后执行索引,或者您需要将视图模型属性从IEnumerable 更改为List
猜你喜欢
  • 2012-03-02
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多