Solved

@Html.DropDownListFor -- ASP.net MVC4

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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