2016-04-13 5 views
5

नहीं है, मुझे अपने मुद्दे के लिए कोई समाधान नहीं मिला, मैंने कई विकल्पों की कोशिश की है लेकिन मैं इसे हल नहीं कर सका।आईनेमरेबल त्रुटि प्राप्त करना: सीएस 1061 में सी # एएसपी.नेट एमवीसी 5

मैं पहले मॉडल के साथ अपना डेटाबेस जेनरेट करता हूं, इसके बाद मैंने दृश्य (इंडेक्स, बनाएं, संपादित करें, हटाएं ..) उत्पन्न करने के लिए मचान का उपयोग किया है। मॉडल के साथ एकमात्र दृश्य (इंडेक्स) IENumerable का उपयोग करें।

सूचकांक देखें था:

@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> 

मैं datasearch के लिए एक अतुल्यकालिक रूप सम्मिलित करना चाहते हैं, ताकि हो जाता है:

@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> 
    } 
} 

मैं के लिए एक दृश्य मॉडल वर्ग को जोड़ने में मॉडल को संशोधित किया है इंडेक्स व्यू को पैरामीटर को ले कर मॉडल देखें मॉडल के रूप में लेने के लिए अनुमति दें और खोज पैरामीटर का उपयोग करें:

//------------------------------------------------------------------------------ 
// <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(); 
     } 
    } 
} 

वर्तमान नियंत्रक

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

लेकिन उस तर्ज पर सूचकांक दृश्य में IEnumerable के बारे में ही प्रकार के दो त्रुटियाँ पैदा करता है:

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

और

if (Model.ListeLocations.Count == 0) 

मैं यह त्रुटि आई :

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 ?)

इसका क्या अर्थ है? मैं इसे कैसे हल करूं? मुझे अभी भी आईनेमरेबल इंटरफ़ेस को समझने में कुछ कठिनाई है।

उत्तर

0

यह जहां अपने त्रुटि है:

var listeTriee = liste.OrderBy(t => t.site_name); 

return View(listeTriee); 

इसके बजाय अपने View लिए एक मॉडल में उत्तीर्ण होने की, तो आप एक संग्रह (IEnumerable) जो वास्तव में संपत्ति ListeLocations नहीं है गुजर रहे हैं।

आप एक viewmodel बनाना चाहिए और वहाँ में संग्रह कर दिया:

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

तो फिर आप अपने नियंत्रक में है कि मॉडल पारित कर सकते हैं:

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); 
    } 

अब ध्यान में रखते हुए अपना चेक काम करेगा:

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

@JeffreyC में AJAX के साथ खोज रूपों बनाने पर थोड़ा और अध्ययन करने के लिए आप सलाह कर सकते हैं। एक आइटम के बजाय एकाधिक आइटम। –

+0

ठीक है, मैंने देखा है, मैंने आपके जैसा संशोधित किया है लेकिन हमेशा त्रुटियां :( –

+0

@ जेफरीसी। मैं आपके मॉडल को 'आईन्यूमेरेबल' आदि से विरासत में देखता हूं, बस सुनिश्चित करें कि आपके पास एक मॉडल है जिसमें * एक संग्रह है, मॉडल नहीं कि * एक * संग्रह है। –

1

आपकी Location कक्षा में गुण Recherche और ListeLocation, लेकिन उस वर्ग के एक आईनेमरेबल में उन गुण नहीं हैं।

आप उस वर्ग के उदाहरण के रूप में कक्षा के IENumerable का उपयोग कर रहे हैं, जो काम नहीं कर सकता है।

आप क्या आप अपने मॉडल होने की जरूरत है, क्योंकि दृश्य के एक हिस्से में आप का उपयोग Model जैसे कि यह एक Location थे, और दूसरे भाग (@foreach(var item in Model) {) में आप एक IEnumerable

रूप में इसका इस्तेमाल बारे में सोचना चाहिए जब आप मॉडल के रूप में IEnumerable इंटरफ़ेस का उपयोग करते हैं, तो आप दृश्य को बता रहे हैं कि आपके पास किसी प्रकार की सूची, संग्रह या कुछ है जिसे आप मॉडल के रूप में 'गणना' कर सकते हैं। आपकी Location ऑब्जेक्ट्स की एक सूची, इसलिए बोलने के लिए, एक भी नहीं। अपनी टिप्पणी के जवाब में

संपादित करें: बदलें यदि आप यो नियंत्रक कार्रवाई को बदलने की जरूरत है @model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>CAD_CMDBv2.Models.RechercheLocationViewModel

फिर रहे हैं:

बजाय:

var liste = db.Locations.ToList(); 
var listeTriee = liste.OrderBy(t => t.site_name); 

return View(listeTriee); 

उपयोग:

var model = new RechercheLocationViewModel(); 
model.AddRange(db.Locations.OrderBy(t => t.site_name)); 

return View(model); 

लेकिन जो इसे 'काम' नहीं करेगा: आपकी खोज क्वेरी दृश्य में नहीं हो सकती है, आपको इसके लिए सर्वर पर वापस जाना होगा, इसलिए आपके मॉडल का आर्किटेक्चर बिल्कुल सही नहीं है; आपको वहां अपने सभी स्थानों की आवश्यकता नहीं है, एक ऐसा Location है जो मुझे समझ में नहीं आता है। यदि आप दृश्य में एसिंक खोज करना चाहते हैं, तो आपको सर्वर पर वापस AJAX कॉल की आवश्यकता है जो खोज परिणाम वापस करने जा रहा है। आपका फॉर्म अब आपके नियंत्रक पर Search कार्रवाई पर कुछ पोस्ट करने जा रहा है, और मुझे नहीं पता कि यह कार्य क्या करता है।

मैं केवल ASP.NET MVC

+0

लेकिन यह कैसे काम करता है? –

+0

यह इस बात पर निर्भर करता है कि आप वास्तव में क्या हासिल करने की कोशिश कर रहे हैं। आपके विचार पर 'foreach' लूप शेष दृश्य के संबंध में मेरे लिए एक रहस्य है। – Stephen

+0

foreach लूप मॉडल के प्रत्येक तत्व को लेने और इंडेक्स में प्रदर्शित करने के लिए एक डेटाटेबल –

संबंधित मुद्दे