?
Solved

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

Posted on 2014-11-05
2
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 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 66

Accepted Solution

by:
Jim Horn earned 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

777 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