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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:48:40+00:00 2026-05-28T20:48:40+00:00

I want to retrieve data from db into a model and pass it to

  • 0

I want to retrieve data from db into a model and pass it to the view

e.g.
1) db State table, has 2 columns Code and Text.
2) my model:

public class state 
{
    public IEnumerable<SelectListItem> list { get; set; }
    public string selected { get; set; } 
}

3) and I want the controller to place code and text data from state table into list and set selected to 1 and return the model to the view

how would I write the loop in the controller

  • 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-28T20:48:41+00:00Added an answer on May 28, 2026 at 8:48 pm
    public class state
    {
        public List<SelectListItem> list { get; set; }
        public string selected { get; set; }
    
        public state()
        {
            SqlConnection DbConn = new SqlConnection(YourConnectionStringHere);
            SqlCommand SelectStates = new SqlCommand();
            SelectStates.CommandText = "select code from state";
            SelectStates.Connection = DbConn;
    
            DbConn.Open()
            SqlDataReader ReadStates = SelectStates.ExecuteReader();
    
            while (reader.Read())
                list.Add(
                    new SelectListItem() { Text = reader[0].ToString(), Value = reader[0].ToString() });
    
            DbConn.Close();
    
            list[0].Selected = true;
        }
    }
    

    Ideally, you could create a data access class to do what that above constructor does, but you should get the general idea of the code.

    As for the Action method:

    public ActionResult StateSelect()
    {
        state YourViewModel = new state();
    
        return View(YourViewModel);
    }
    

    A corresponding View:

    @model state
    
    @* Add your other markup here *@
    
    @Html.ListBoxFor(m => m.selected, Model.list)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the next code to retrieve data from a table in the database:
i retrieve data from upc database and then want to insert the data into
I have DropDownList with CascadingDropDown extender. If I want to retrieve data from the
I want to retrieve all records from one table when there are no matches
Having created my model, view and controller in an attempt to get data from
I have an XML file and I want to retrieve data from it so
I'm trying to retrieve data from an SQL Server 2000 server, and place into
I want to generate some formatted output of data retrieved from an MS-Access database
I have a scripts that retrieves huge data on a table. I want to
I want to retrieve a list of the files that has been added or

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.