Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

28 Experts available now in Live!

Get 1:1 Help Now