[英]Getting IEnumerable Error: CS1061 does not contain C# ASP.NET MVC5
I couldn't find a solution for my issue, I have tried many alternatives but I could not resolve it. 我无法找到解决问题的方法,我尝试了很多替代方案,但我无法解决。
I generate my database with the model first, afterwards I have used Scaffolding to generate the Views (Index, Create, Edit, Delete..). 我首先使用模型生成我的数据库,之后我使用Scaffolding生成视图(索引,创建,编辑,删除..)。 The only view (Index) with the model use IEnumerable.
唯一的视图(索引)与模型使用IEnumerable。
The Index View was : 索引视图是:
@model IEnumerable<CAD_CMDBv2.Models.Location>
@{
ViewBag.Title = "Location's Management";
}
<h2>All Locations</h2>
<p>
@Html.ActionLink("Create Location", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Location.site_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.country_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.region_name)
</th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Location.site_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.country_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.region_name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Location.location_id }) |
@Html.ActionLink("Details", "Details", new { id = item.Location.location_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Location.location_id })
</td>
</tr>
}
</table>
I want to insert an asynchronous form for the datasearch, so that becomes: 我想为数据集插入一个异步表单,这样就变成:
@model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>
@{
ViewBag.Title = "Location's Management";
}
<h2>All Locations</h2>
<p>
@Html.ActionLink("Create Location", "Create")
</p>
@using (Html.BeginForm("Search", "Restaurant", FormMethod.Get))
{
@Html.TextBoxFor(r => r.Recherche)
<input type="submit" value="Rechercher" />
<p>Search Results </p>
if (Model.ListeLocations.Count == 0)
{
<p> No Results but you can create it !</p>
}
else
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Location.site_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.country_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.region_name)
</th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Location.site_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.country_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.region_name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Location.location_id }) |
@Html.ActionLink("Details", "Details", new { id = item.Location.location_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Location.location_id })
</td>
</tr>
}
</table>
}
}
I have modified the model in adding a View Model class to allow in IndexView to take as model the View Model by taking over the parameters Locations and use the Search parameter: 我在添加View Model类时修改了模型,以允许IndexView通过接管参数Locations并使用Search参数作为View Model的模型:
//------------------------------------------------------------------------------
// <auto-generated>
// Ce code a été généré à partir d'un modèle.
//
// Des modifications manuelles apportées à ce fichier peuvent conduire à un comportement inattendu de votre application.
// Les modifications manuelles apportées à ce fichier sont remplacées si le code est régénéré.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CAD_CMDBv2.Models
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Location
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Location()
{
this.User = new HashSet<User>();
this.host = new HashSet<Host>();
this.client_catia = new HashSet<Client_catia>();
this.client_smartam = new HashSet<Client_smarteam>();
}
public int location_id { get; set; }
[Display(Name = "Site's Name")]
public string site_name { get; set; }
[Display(Name = "Country's Name")]
public string country_name { get; set; }
[Display(Name = "Region's Name")]
public string region_name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<User> User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Host> host { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Client_catia> client_catia { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Client_smarteam> client_smartam { get; set; }
}
public class RechercheLocationViewModel : IEnumerable<Location> {
public string Recherche {get; set;}
public Location Location { get; set; }
public List<Location> ListeLocations;
public IEnumerator<Location> GetEnumerator()
{
return ListeLocations.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ListeLocations.GetEnumerator();
}
}
}
The current Controller 目前的控制器
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 CAD_CMDBv2.Models;
namespace CAD_CMDBv2.Areas.Locations.Controllers
{
public class LocationsController : Controller
{
private ModeleDonneesCMDBContext db = new ModeleDonneesCMDBContext();
// GET: Locations/Locations
public ActionResult Index()
{
var liste = db.Locations.ToList();
var listeTriee = liste.OrderBy(t => t.site_name);
return View(listeTriee);
}
...
But that generates two errors of the same type about IEnumerable in the Index View on the lines: 但是在行索引视图中会产生两个关于IEnumerable的相同类型的错误:
@Html.TextBoxFor(r => r.Recherche)
And 和
if (Model.ListeLocations.Count == 0)
I got this error: 我收到了这个错误:
CS1061 Error 'IEnumerable' does not contain a definition for 'ListeLocations' and no extension method 'ListeLocations' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference ?)
CS1061错误'IEnumerable'不包含'ListeLocations'的定义,并且没有扩展方法'ListeLocations'可以找到接受类型'IEnumerable'的第一个参数(你是否缺少using指令或汇编引用?)
What does that mean? 这意味着什么? How can I resolve this?
我该如何解决这个问题? I still have some difficulty with understanding the IEnumerable interface.
理解IEnumerable接口仍然有些困难。
Your Location
class contains the properties Recherche
and ListeLocation
, but an IEnumerable of that class does not have those properties. 您的
Location
类包含Recherche
和ListeLocation
属性,但该类的IEnumerable没有这些属性。
You are using the IEnumerable of the class as an instance of that class, that can't work. 您正在使用该类的IEnumerable作为该类的实例,这是无法工作的。
You should think about what you need your model to be, because in one part of the view you use Model
as if it were a Location
, and in another part ( @foreach(var item in Model) {
) you use it as an IEnumerable 你应该考虑你的模型需要什么,因为在视图的一个部分你使用
Model
就像它是一个Location
,而在另一个部分( @foreach(var item in Model) {
)你将它用作IEnumerable
When you use the IEnumerable
interface as a model, you are telling the View you have some kind of list, collection, or something you can 'Enumerate' as a model. 当您使用
IEnumerable
接口作为模型时,您告诉View您有某种列表,集合或可以“枚举”为模型的东西。 A list of your Location
objects, so to speak, not a single one. 可以说,您的
Location
对象列表不是单个对象。
Edit in response to your comments: Change the @model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>
to CAD_CMDBv2.Models.RechercheLocationViewModel
编辑以响应您的评论:将
@model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>
更改为CAD_CMDBv2.Models.RechercheLocationViewModel
Then you need yo change the controller Action: 然后你需要你改变控制器动作:
Instead of : 代替 :
var liste = db.Locations.ToList();
var listeTriee = liste.OrderBy(t => t.site_name);
return View(listeTriee);
use: 采用:
var model = new RechercheLocationViewModel();
model.AddRange(db.Locations.OrderBy(t => t.site_name));
return View(model);
But that won't make it 'work': Your search query cannot take place in the view, you will have to go back to the server for that, so the architecture of your model is not quite right; 但这不会使它“工作”:您的搜索查询无法在视图中进行,您将不得不返回服务器,因此您的模型的架构不太正确; you don't need all your locations in there, an what the single
Location
is there for I don't understand as well. 你不需要在那里的所有位置,我不明白的单一
Location
是什么。 If you want to do an async search in the view, you need an AJAX call back to the server that's going to return the search result. 如果要在视图中执行异步搜索,则需要向将返回搜索结果的服务器发回AJAX回调。 Your form is now just going to post something back to a
Search
action on your controller, and I don't know what that action does. 您的表单现在只是将某些内容发布回控制器上的“
Search
操作,我不知道该操作的作用。
I can only advice you to study a bit more on creating search forms with AJAX in ASP.NET MVC 我只能建议你在ASP.NET MVC中用AJAX创建搜索表单
This is where your error is: 这是您的错误所在:
var listeTriee = liste.OrderBy(t => t.site_name);
return View(listeTriee);
Instead of passing a single model to your View
, you are passing a collection ( IEnumerable
) which indeed doesn't have the property ListeLocations
. 您没有将单个模型传递给
View
,而是传递一个集合( IEnumerable
),它确实没有属性ListeLocations
。
You should create a viewmodel and put the collection in there: 您应该创建一个viewmodel并将集合放在那里:
public class ListeTrieeViewModel
{
...
public IEnumerable<Locations> ListeLocations {get; set;}
}
Then you can pass that model in your controller: 然后你可以在你的控制器中传递该模型:
public ActionResult Index()
{
var liste = db.Locations.ToList();
var listeTriee = liste.OrderBy(t => t.site_name);
var viewModel = new ListeTrieeViewModel { ListeLocations = listeTriee; }
return View(viewModel);
}
Now your check in the view will work: 现在,您在视图中的检查将起作用:
if (Model.ListeLocations.Count() == 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.