Getting IEnumerable Error: CS1061 does not contain C # ASP.NET MVC5

I could not find a solution to my problem, I tried many alternatives, but I could not solve it.

First I create my database with the model, after which I used Scaffolding to create views (Index, Create, Edit, Delete ..). The only view (index) with the model uses IEnumerable.

Index Type:

@model IEnumerable<CAD_CMDBv2.Models.Location> @{ ViewBag.Title = "Location 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 datasearch so that it becomes:

 @model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel> @{ ViewBag.Title = "Location 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 changed the model when adding the View Model class to allow IndexView to take the View Model as a model, taking the Locations parameters and using the Search parameter:

 //------------------------------------------------------------------------------ // <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 Name")] public string site_name { get; set; } [Display(Name = "Country Name")] public string country_name { get; set; } [Display(Name = "Region 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(); } } } 

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 this gives rise to two errors of the same type in IEnumerable in the representation of the index in rows:

 @Html.TextBoxFor(r => r.Recherche) 

and

 if (Model.ListeLocations.Count == 0) 

I got this error:

CS1061 The error "IEnumerable" does not contain the definition of "ListeLocations" and the extension method "ListeLocations" accepts the first argument of the type "IEnumerable" can be found (do you have a using directive or an assembly reference?)

What does it mean? How can i solve this? It’s still hard for me to understand the IEnumerable interface.

+5
source share
2 answers

Your Location class contains the Recherche and ListeLocation , but the IEnumerable of this class does not have these properties.

You are using an IEnumerable class as an instance of this class that cannot work.

You should think about what your model is needed for, because in one part of the view you use Model as if it were Location , and in the other part ( @foreach(var item in Model) { ) you use it as IEnumerable

When you use the IEnumerable interface as a model, you tell View that you have some kind of list, collection, or something that you can "Enumerate" as a model. The list of your Location objects, so to speak, is not one.

Edit in response to your comments: Change @model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel> to CAD_CMDBv2.Models.RechercheLocationViewModel

Then you need to change the controller. Act:

Instead:

 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 this will not make it “work”: your search query cannot take place in the view, for this you will need to return to the server, so the architecture of your model is not entirely correct; you don’t need all your locations, which is the only Location for which I don’t understand either. If you want to perform an asynchronous search in a view, you need an AJAX response to the server, which will return the search result. Your form is now about to send something back to the Search action on your controller, and I don't know what the action is.

I can advise you to work a bit on creating search forms using AJAX in ASP.NET MVC

+1
source

This is where your mistake 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 really does not have the ListeLocations property.

You must create a view model and put the collection there:

 public class ListeTrieeViewModel { ... public IEnumerable<Locations> ListeLocations {get; set;} } 

Then you can pass this 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 view will work:

 if (Model.ListeLocations.Count() == 0) 
0
source

Source: https://habr.com/ru/post/1247022/


All Articles