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();
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.