Solved

@Html.DropDownListFor -- ASP.net MVC4

Posted on 2013-10-29
1
729 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

21 Experts available now in Live!

Get 1:1 Help Now