Solved

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

Posted on 2014-11-05
2
256 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
ID: 40426631
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
ID: 40426746
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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