Solved

@Html.DropDownListFor -- ASP.net MVC4

Posted on 2013-10-29
1
727 Views
Last Modified: 2013-11-13
My create.cshtml starts with "@model MWR.Models.MAINT_WORK_REQ"
which displays everything in the "MAINT_WORK_REQ" database
table and the below STATIC selections

                 Html.DropDownList("ProjectID", new List<SelectListItem>
                 {
                    new SelectListItem{ Text="CST031 - LeftHand", Value = "CST031 - LeftHand" },
                    new SelectListItem{ Text="CST031 - RightHand", Value = "CST031 - RightHand" }
                 }
------------------------------------------
How can I get the above to something like
@Html.DropDownListFor(viewModel => viewModel.ProjectID, ProjectID)
so it displays values from the below SQL ?

select ProjectID
from Lookup_ProjectID
where ProgramID = 'TEST1'
0
Comment
Question by:finance_teacher
1 Comment
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39610953
I do not know how you get data from your db, but i will assume Entity Framework and that you have a datacontext object for your db entities.

In your MWR.Models.MAINT_WORK_REQ code add another property:

public class MAINT_WORK_REQ
{
    ... // Here are the rest of your model's properties
    public SelectList Projects {get;set;}
    // If you have a public constructor add the following on it
    public MAINT_WORK_REQ()
    {
        using(datacontext ctx = new datacontext())
        {
            Projects = 
                new SelectList
                (
                    ctx
                    .Lookup_ProjectID
                    .Where(x=>x.ProgramID == "TEST1")
                    .Select(x => new { x.ProjectID, x.ProgramID }), 
                    "ProjectID", 
                    "ProgramID"
                );
        }
    }
}

Open in new window


In this case in your View have the following code:

@Html.DropDownListFor(viewModel => viewModel.ProjectID, viewModel.Projects)

Open in new window


Alternatively you may have a static method that returns the list:

public class MAINT_WORK_REQ
{
    ... // Here are the rest of your model's properties
    // I added an argument in order to not hardcode "Test1"
    public static SelectList GetProjectList(string pID)
    {
        using(datacontext ctx = new datacontext())
        {
            return  new SelectList
                (
                    ctx
                    .Lookup_ProjectID
                    .Where(x=>x.ProgramID == pID)
                    .Select(x => new { x.ProjectID, x.ProgramID }), 
                    "ProjectID", 
                    "ProgramID"
                );
        }
    }
}

Open in new window


In this case in your View you would have to call the method.:

@Html.DropDownListFor(viewModel => viewModel.ProjectID, viewModel.GetProjectList("Test1"))

Open in new window


Keep in mind that in the first case the SelectList Projects depends on the initialization of the object and it is defined at that point, while in the second case you can call it any time you like. Another difference is that in the first case the call to the db happens only once at the initialization (and then you use the preLoaded property), while in the second case you use the db each time you add a Projects list in your view.

In the first case i have "Test1" hardcoded, but this doesn't mean you cannot add a parameter in the initialization of the object...

Giannis

Which way you are going to use it depends on your whole project.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

13 Experts available now in Live!

Get 1:1 Help Now