Solved

How Do You Load DropDown Values From a Database Using a MVC Razor Setup?

Posted on 2014-11-05
2
247 Views
Last Modified: 2014-11-06
How do you load values from a database table to a dropdown using MVC Razor View and Microsoft SQL Server?

Thanks,

Dan
0
Comment
Question by:danielolorenz
2 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
Comment Utility
The better approach depends on how you have everything set up.

One solution would be to use a view model, and have a property that returns a list of SelectListItem objects, that you can bind in the view.

Example:

@Html.DropDownListFor(model => model.State, model.StateList)
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
No clue on the MVC part, but like Bob I've used views, or sometimes Stored Procedures, to generate lists that are connected to a dropdown.  

You can even pull off an <ALL> like this...
SELECT id, label
FROM (
   SELECT -1 as id, '<ALL>' as label, 1 as sort_order
   UNION
   SELECT id, column_name, 2 as sort_order
   FROM source_table) a
ORDER BY sort_order

Open in new window


Then in your view or stored proc handle the -1 as 'ALL' like this...
WHERE (@id = -1 OR @id = id) 

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 34
Refresh Datagridview from another form 1 25
How do I bind the results to a grid 3 22
Mssql SQL query 14 26
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now