Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:02:20+00:00 2026-05-10T20:02:20+00:00

Im having problems displaying records to my view when passing viewdata to a user

  • 0

Im having problems displaying records to my view when passing viewdata to a user control. This is only apparent for linq to sql objects where I am using table joins.

The exception I receive is ‘Unable to cast object of type ‘<>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.Double]]’ to type App.Models.table1.’

I have searched for a fix to this issue but not too familiar on whats wrong here for me to search for the right subject. This should be working in theory and this works for single table retrieving but when I added a join in their I ran into problems. I am currently using a foreach statement to query through my data via single table declaration. Any help would be greatly appreciated. Thanks in advance.

My current setup is:

CViewDataUC.cs(my class to hold viewdata 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> <%}%> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:02:20+00:00Added an answer on May 10, 2026 at 8:02 pm
    foreach (table1 m in (IEnumerable)ViewData.Model) 

    m is not of type table1. It is an anonymous type (select new { ... } in CViewDataUC.cs).

    You should create a class that represents the type of the model objects you are passing from controller to view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've not encountered any issues using the YUI Calendar widget… May 12, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer As you know, javascript paging kills the back button. But… May 12, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer One possibility is to wrap TxRx in a mockable class… May 12, 2026 at 12:33 am

Related Questions

I'm creating a binary file to transmit to a third party that contains images
Problem description: In my application, I have to present the contents of data packets
I'm developing an Access 2003 Database that uses a MS SQLServer backend. I'm trying
I'm having some problems displaying the contents of one NSArrayController in two windows defined

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.