Solved

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

Posted on 2014-11-05
2
254 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSAS Hierarchy with columns with folder names 10 19
RegEx with optional part 4 42
query optimization 6 14
SQL Recursion schedule 13 14
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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!
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

809 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