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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:28:20+00:00 2026-06-04T04:28:20+00:00

I wrote model class which returns a List then i pass it to view,

  • 0

I wrote model class which returns a List then i pass it to view, but when the page is requested the view call the model many times and then crash the page, here’s my code please help me I am new to asp.net MVC

Model

public List<string> DoWork()
    {
        List<string> results = new List<string>();
        using (SqlConnection con = new SqlConnection(@"Connection String Here"))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand(@"SELECT Column1 FROM Table1", con))
            {
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        results.Add(rdr.GetString(0));
                    }
                }
            }
        }
        return results;
    }

Controller

 public ActionResult Index()
    {
        MyData data = new MyData();
        return View(data);
    }

View

<ul>
@for (int i = 0; i <@Model.DoWork().Count; i++)
{
<li>
  @Model.DoWork()[i]
</li>
}
</ul>
  • 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-06-04T04:28:20+00:00Added an answer on June 4, 2026 at 4:28 am

    The idea is that the DB layer and View layer should be separated.
    This being said, you should get your DB entities in your controller action method, not in the view, and use a ViewModel pattern to spit it to the view.In your case, you have a simple view model, IEnumerable, but it can be any entity.

    public ActionResult Index()
        {
            MyData data = new MyData();
            IEnumerable<string> thelist = data.DoWork();
            return View(thelist);
        }
    

    and in yout View, use the IEnumerable as your model, and loop though it , not calling the DB to fetch your data:

    @model IEnumerable<string>
    ......
    @foreach(string s in Model)
    {
      <li>@s<li>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a common method which returns any model like product, sales,etc.
i have created the folloiwng view model class public class viewmodelclass { public IEnumerable<Question>
I wrote the model like this: class Book(models.Model): title = models.CharField(max_length = 200) author
I wrote a data model, into one class I need to merge 3 arrays.
I have wrote a class (a snippet is below) which have some data members
I have a class which is responsible and the owner of building my model
Here is a related manager I wrote: class PortfolioItemManager(models.Manager): use_for_related_fields = True def extended(self):
Salut, community! Briefly. I wrote usual model over QAbstractTableModel and using it in usual
I wrote some code for the ising model in python (2d). Everything looks seems
I created a custom view that uses Json.Simple to serialize the model and write

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.