MVC LINQ to SQL Join Record Display Table

I am having problems displaying entries in my view when passing viewdata to a user control. This is only visible for linq to sql objects, where I use table joins.

The exception I get is "Unable to transfer object of type" <> f__AnonymousType4 10[System.String,System.Int32,System.Nullable1 [System.DateTime], System.String, System.String, System.String, System.String, System.String, System.Nullable 1[System.Single],System.Nullable1 [System. Double]] 'to enter App.Models.table1. "

I was looking for a fix for this problem, but I did not know very well what was wrong here to find a suitable object. This should work in theory, and it works to restore a single table, but when I added a join to them, I ran into problems. I am currently using the foreach statement to query through my data using a separate table declaration. Any help would be greatly appreciated. Thanks in advance.

My current setup:

CViewDataUC.cs (my class for storing views and data connections specifically for user controls)

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>
<%}%>
+2
source share
3 answers
foreach (table1 m in (IEnumerable)ViewData.Model)

m table1. (select new { ... } CViewDataUC.cs).

, , , .

+3

, . , , , : P. , MVC LINQ to SQL, : :

  • CInformation.cs ,

    CInformation {

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

    }

  • public void uc_VDGenInfoDC (ViewDataDictionary viewData, 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;
    
    }
    
  • foreach (CInformation m in (IEnumerable) ViewData.Model) {

     m.column1
     m.column2
     ...
    

    }

+2

Thanks, it helped me a lot!

One small comment, you should use angle brackets:

IQueryable<CInformation> info = from table1 in dataContext.table1
+1
source

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


All Articles