Frage

Im Problem Aufzeichnungen meiner Ansicht anzuzeigen, wenn Bildschirmtextsystem auf eine Benutzersteuerung übergeben. Dies ist nur ein scheinbares für LINQ to SQL-Objekte, wo ich bin mit Tabelle beitritt.

Die Ausnahme, die ich empfangen „Kann nicht das Objekt des Typs‚<> f__AnonymousType410[System.String,System.Int32,System.Nullable1 [System.DateTime], System.String, System.String, System.String, System.String, System.String, System.Nullable1[System.Single],System.Nullable1 [System werfen ‚.Double]] eingeben App.Models.table1."

Ich habe für ein Update für dieses Problem gesucht, aber auch nicht vertraut auf, was falsch hier für mich nach dem richtigen Thema zu suchen. Dies sollte in der Theorie funktionieren und das funktioniert für einzelne Tabelle retrieving, aber wenn ich eine Verknüpfung hinzugefügt in ihrem Ich lief in Probleme. Ich bin derzeit eine foreach-Anweisung durch meine Daten über einzelne Tabellendeklaration abzufragen. Jede Hilfe wäre sehr geschätzt. Vielen Dank im Voraus.

Meine aktuelle Setup ist:

CViewDataUC.cs (meine Klasse Bildschirmtextsystem und Datenverbindungen speziell für Benutzersteuerungen zu halten)

public void Info(ViewDataDictionary viewData, int id)
    {
        var dataContext = new testDataContext();

        var info = from table1 in dataContext.table1
                   join table2 in dataContext.table2 on table1.type_id equals table2.type_id
                   join table3 in dataContext.table3 on table1.id equals table3.id
                   join table4 in dataContext.table4 on table1.id equals table4.id
                   where table1.id == id
                   select new
                   {
                       table1.column1,
                       table1.column2,
                       table1.column3,
                       table1.column4,
                       table1.column5,
                       table1.column6,
                       table1.column7,
                       table2.column1,
                       table3.column1,
                       table4.column1
                   };         

        viewData["vd_Info"] = info;

    }

HomeController.cs (Controller)

public ActionResult Information(int id)
        {
            ViewData["Title"] = "Information";


            CViewDataUC o_info = new CViewDataUC();

            o_info.Info(this.ViewData, id);


            return View();
        }

Information.aspx (View)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"
    CodeBehind="Info.aspx.cs" Inherits="App.Views.Info" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%Html.RenderPartial("~/Views/UserControls/Info.ascx", ViewData["vd_Info"]);%>
</asp:Content>

Info.ascx (User Control)

<%foreach (table1 m in (IEnumerable)ViewData.Model)
  { %>
<div class="left">
    <br />
    <br />
    <p id="medium">
        Column 1
        <br />
        <%= Html.TextBox("column1", m.column1, new {@class = "textBox", @readonly = "readonly" })%>
        Column 1
        <br />
        <%= Html.TextBox("column2", m.column2, new {@class = "textBox", @readonly = "readonly" })%>
        <br />
        Column 1
        <br />
        <%= Html.TextBox("column3", m.column3, new {@class = "textBox", @readonly = "readonly" })%>
                </p>
</div>
<%}%>
War es hilfreich?

Lösung

foreach (table1 m in (IEnumerable)ViewData.Model)

m ist nicht vom Typ table1. Es ist ein anonymer Typ (select new { ... } in CViewDataUC.cs).

Sie sollten eine Klasse erstellen, die die Art des Modells repräsentieren Objekte, die Sie von der Steuerung sind vorbei anzuzeigen.

Andere Tipps

Danke, das wie ein Charme. Ich habe das Problem völlig übersehen, ich denke, es ist ein Montag, dass die Gründe: P. Aber für die Menschen, die in dem gleichen Problem laufen und sind noch neu in MVC und LINQ, das Problem SQL gelöst tut einfach diese Schritte:

  1. Erstellen Sie eine Klasse CInformation.cs Variablen hinzufügen similiar auf Ihre Anfrage

    public class CInformation {

    public CInformation() { }
    
    public string _column1{ get; set; }
    public string _column2{ get; set; }
    ...
    

    }

  2. In Ihrer Anfrage

    public void uc_VDGenInfoDC (Viewdatadictionary Bildschirmtextsystem, int id)     {

        var dataContext = new testDataContext();
    
        IQueryable<CInformation> info = from table1 in dataContext.table1
                                        join table2 in dataContext.table2 on table1.type_id equals table2.type_id        
                                        join table3 in dataContext.table3 on table1.id equals table3.id                  
                                        join table4 in dataContext.table4 on table1.id equals table4.id                       
                   where table1.test_id == id
                   select new CInformation
                   {
                       _column1 = table1.column1.ToString(),
                       _column2 = table2.column1.ToString(),
                       ...
                   };         
    
        viewData["vd_Info"] = info;
    
    }
    
  3. In Ihrer Benutzerkontrolle oder Ansicht

    foreach (CInformation m (IEnumerable) ViewData.Model) {

     m.column1
     m.column2
     ...
    

    }

Danke, hat dies hat mir sehr geholfen!

Ein kleiner Kommentar, Sie haben spitze Klammern verwenden:

IQueryable<CInformation> info = from table1 in dataContext.table1
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top