2016-11-21 7 views
0

Ich erhalte einen Fehler, bei dem ich die Lösung nicht finden kann, es ist wahrscheinlich ein Problem meiner Modelle, die ich versuche zu verbinden, Der Fehler, den ich bekomme, ist folgender: "Eine Ausnahme vom Typ 'System.ArgumentOutOfRangeException' ist in mscorlib.dll aufgetreten, wurde aber nicht im Benutzercode behandelt", wenn ich versuche, jede Spalte zu durchlaufen. Also hat eine der Tabellen weniger Datensätze als die anderen? Wie löse ich das? Die Iteration ist in der Ansicht -> item.Contacts.Count()asp.net mvc 5 Eine Ausnahme vom Typ 'System.ArgumentOutOfRangeException' ist aufgetreten

Das Programm soll einige Spalten der Auswahl von Tabellen nehmen: Contatti, Kontakte und Firmen, die an verschiedenen Standorten sind und bringen sie zusammen (die in die Liste der GET-Methode im Controller), Contatti ist eine SQL-Server-Tabelle, die als .edmx in das Projekt importiert wird, die anderen 2 sind lokal für die im Projekt gesetzten Tabellen. der Code ist folgendes:

Modell, das die Tabellen zusammen bringt:

public partial class ContactsUni2 
{ 

    [Key, ForeignKey("Contatti")] 
    public int ContattoID { get; set; } 
    public List<Contatti> Contattis { get; set; } 
    public List<Companies> Companies { get; set; } 
    public List<Contact> Contacts { get; set; } 
    public virtual Contatti Contatti { get; set; } 
    public virtual Contact Contact { get; set; } 
    public virtual Companies Company { get; set; } 
} 

Kontakt Modell:

public class Contact 
{ 
    [Key] 
    public int ContactId { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Zip { get; set; } 
    [DataType(DataType.EmailAddress)] 
    public string Email { get; set; } 
    [ForeignKey("Companies")] 
    public int CompanyId { get; set; } 
    public virtual ICollection<Companies> Companies { get; set; } 
    [Required] 
    //public virtual Contatti Contatti { get; set; } 
    public virtual ICollection<Contatti> Contatti { get; set; } 
} 

Firmen Modell:

public class Companies 
{ 
    [Key] 
    public int CompanyId { get; set; } 
    public string CompanyName { get; set; } 
    public string CompanyAddress { get; set; } 
    public string CompanyCity { get; set; } 
    public string CompanyState { get; set; } 
    public string CompanyZip { get; set; } 
    public string CompanyArea { get; set; } 
} 

Contatti Modell:

public partial class Contatti 
{ 
    [Key, ForeignKey("Contact")] 
    public int ContattoID { get; set; } 
    public string Nome { get; set; } 
    public string Via { get; set; } 
    public string Citta { get; set; } 
    public string Stato { get; set; } 
    public string CodicePostale { get; set; } 
    public string Email { get; set; } 
    [Required] 
    public virtual ICollection<Contact> Contact { get; set; } 
} 

Controller-GET:

// GET: ContactsUni21 
    public ActionResult Index(String Page) 
    { 
     ContactsUni2 CU = new ContactsUni2(); 
     CU.Contattis = db.Contattis.ToList(); 
     CU.Contacts = db.Contacts.ToList(); 
     CU.Companies = db.Companies.ToList(); 
     List<ContactsUni2> contactlist = new List<ContactsUni2>(); 
     contactlist.Add(CU); 
     return View(contactlist); 
    } 

Ausblick:

<body> 
<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Contatti.ContattoID) 
      @Html.DisplayNameFor(model => model.Contatti.Nome) 
      @Html.DisplayNameFor(model => model.Contatti.Citta)    
      @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.ToList()) 
    { 
     for (int i = 0; i < @item.Contacts.Count(); i++) 
     { 
      <tr> 
       <td> 
        @item.Contattis[i].ContattoID 
        @item.Contattis[i].Nome 
        @item.Contattis[i].Citta            
        @item.Contattis[i].CodicePostale 
        @item.Contattis[i].Email 
        @item.Contacts[i].Address 
        @item.Contacts[i].CompanyId 
        @item.Contacts[i].ContactId 


       </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> 
+0

„Wie kann ich lösen Dies?" Welches Problem? Dass die Kollektionen unterschiedliche Größen haben oder dass du mehr Gegenstände ziehen willst, als sie sind? Warum nicht einfach beide Sammlungen einzeln durchlaufen? –

+1

Wahrscheinlich (int i = 0; i <@ item.Contacts.Count(); i ++) i ist größer als die Menge von Contattis (@ item.Contattis [i]). Mit anderen Worten, Contattis hat weniger Elemente als Kontakte. – kat1330

+0

Ich habe versucht, getrennt zu loopen, aber ich bekomme nur die Daten aus der Contacts-Tabelle, nicht von Contattis, ist es, weil es eine SQL-Server-Tabelle oder das Datenmodell ist falsch? – Dantuzzo

Antwort

0

Ich habe noch keine Gelegenheit gehabt, dies zu überprüfen, aber so etwas wie dies:

@foreach (var item in Model.ToList()) 
{ 
    //for (int i = 0; i < @item.Contacts.Count(); i++) 
    foreach (var contact in item) 
    { 
     <tr> 
      <td> 
       @contact.ContattoID 
       @contact.Nome 
       @contact.Citta            
       @contact.CodicePostale 
       @contact.Email 
       @contact.Address 
       @contact.CompanyId 
       @contact.ContactId 


      </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> 
    } 
} 
Verwandte Themen