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

  • Home
  • SEARCH
  • 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 6803077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:18:02+00:00 2026-05-26T19:18:02+00:00

I very new to .NET and Entity Framework, and I have a problem with

  • 0

I very new to .NET and Entity Framework, and I have a problem with my code (below). I am getting the following error:

        Unable to cast object of type '<>f__AnonymousType1`2[ 
    SamWinInterface.Models.tbl_interface_category,
    SamWinInterface.Models.tbl_interface_menu]' to type
'SamWinInterface.Models.tbl_interface_menu'.

This is my code:

public ActionResult Index(int id=-1)
{
    ViewBag.Menus = from menu in _db.tbl_interface_menu 
                    join cat in _db.tbl_interface_category on 
                    menu.fld_category_id equals cat.id where 
                    cat.fld_customer_id == id select new { cat, menu }; 

    return View();
}

I’m trying to get menus depending on which category is chosen.

Something like:

<% foreach (tbl_interface_menu m in (IEnumerable)ViewBag.Menus)
   { %>

    <%=  m.fld_section2_title %>

<% } %> 

but I’m getting the above error. How can I get the menus?

  • 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. Editorial Team
    Editorial Team
    2026-05-26T19:18:03+00:00Added an answer on May 26, 2026 at 7:18 pm

    You cannot pass anonymous objects to views. This doesn’t work because anonymous types are emitted as internal. And because ASP.NET views are compiled into a separate assembly at runtime they cannot access those times because they reside in a different assembly. This basically means that an anonymous object that you have defined in your controller action cannot be accessed in your view.

    So as always in an ASP.NET MVC application start by defining view a model:

    public class MyViewModel
    {
        public Category Category { get; set; }
        public Menu Menu { get; set; }
    }
    

    then have your controller action fill this view model and pass it to the view:

    public ActionResult Index(int id=-1)
    {
        var model = 
            from menu in _db.tbl_interface_menu 
            join cat in _db.tbl_interface_category 
            on menu.fld_category_id equals cat.id 
            where cat.fld_customer_id == id 
            select new MyViewModel { Category = cat, Menu = menu }; 
    
        return View(model);
    }
    

    and finally have a strongly typed view:

    <%@ Page 
        Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>" 
    %>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <% foreach (var item in Model) { %>
        <%= item.Menu.fld_section2_title %>
    <% } %>
    
    </asp:Content>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to ADo.net entity framework and i am getting the following
I am very new to the entity framework, so please bear with me... How
I have an ASP.NET application using an entity framework model. In an import routine,
I have written the following code in my .net project. I've never used the
I am new in asp.net MVC, and in Entity Framework to. I watch on
I'm very new in .net c#, just a question here to about the following
I am working with Entity Framework Code first. I have a simple model: public
[ I am new to ADO.NET and the Entity Framework, so forgive me if
Please note that I am very new to .NET and ASP.NET. I have a
I use Entity Framework 4.1, SQL Server 2008 & .NET 4.0 I have a

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.