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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:23:57+00:00 2026-06-09T11:23:57+00:00

I’m new in MVC3 and trying learn something. using Razor view engine. mysql database.

  • 0

I’m new in MVC3 and trying learn something. using Razor view engine. mysql database. I have a table where I keep the companies(companyID, companyName, etc.). I have a model with same attributes :

public class CompanyModel{
    public int companyID;
    public string companyName;
    ......
    ....
}

I want to create a dropdownlist which holds the companies, shows me the company name and when I select an item I need to be able to access the companyID. I can query the table and have all rows in a List of companies :

List<Company> companies = new List<Company>();
companies = getCompanies();

but I don’t know how to bind this list with the dropdownlist and how to get the selected value after call of a post method from my controller.

Any help would be appreciated.

  • 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-09T11:24:00+00:00Added an answer on June 9, 2026 at 11:24 am

    You could use a view model:

    public class MyViewModel
    {
        [DisplayName("Company")]
        public int CompanyId { get; set; }
        public IEnumerable<SelectListItem> Companies { get; set; }
    }
    

    and then have your controller action instantiate, populate and pass this view model to the view:

    public class CompaniesController: Controller
    {
        public ActionResult Index()
        {
            List<Company> companies = getCompanies();
    
            var model = new MyViewModel();
            model.Companies = companies.Select(x => new SelectListItem
            {
                Value = x.companyID.ToString(),
                Text = x.companyName
            });
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            // model.CompanyId will contain the selected value here
    
            return Content(
                string.Format("You have selected company id: {0}", model.CompanyId)
            );
        }
    }
    

    and finally a strongly typed view in which you could render an HTML form containing the dropdown list:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.CompanyId)
        @Html.DropDownListFor(x => x.CompanyId, Model.Companies)
        <button type="submit">OK</button>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't

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.