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 7531749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:15:25+00:00 2026-05-30T05:15:25+00:00

I have a ASP.Net MVC app with Razor. I try to access a collection

  • 0

I have a ASP.Net MVC app with Razor. I try to access a collection in the view. Here is my code:

@foreach (var question in ViewBag.Questions) {
    <p>@question.Name</p>

    foreach (var answer in question.Answers) {
        <input type="radio" name="@answer.QuestionId" value='@answer.id' /> @answer.Text<br />
    }
}

At foreach (var answer in question.Answers) I get:

“The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”

I have a .ToList() when it comes to the Questions collection but what do I do with the Answers collection if I want to access ti at runtime?

  • 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-30T05:15:26+00:00Added an answer on May 30, 2026 at 5:15 am

    Make sure you have eagerly loaded the Answers collection in your controller:

    public ActionResult Foo()
    {
        ViewBag.Questions = db.Questions.Include("Answers").ToList();
        return View();
    }
    

    And concerning ViewBag, well, you could have used a view model instead. Not to mention that your problem is purely related to the data access technology you are using (I suppose EntityFramework) and has nothing to do with ASP.NET MVC.

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

Sidebar

Related Questions

In my ASP.net MVC App (using Razor views) I have a ProductDetails view. This
I have an ASP.NET MVC app that opens a Request view in a new
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I have an ASP.NET MVC App, which use EF code First, for some reason
I have a partial view in an ASP.NET MVC app: @Html.Partial(_Comments, Model) I want
'm using ASP.NET MVC 3 with Razor views. I have a partial view which
I just upgraded an ASP.NET MVC app to version 3.0 and Razor. I have
I have an asp.net mvc app running on a local iis website that is
I have an asp.net mvc app that is built to run as standard web
I have an ASP.NET MVC app and I want to add to each page

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.