Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

@Html.DropDownListFor -- ASP.net MVC4

Posted on 2013-10-29
1
Medium Priority
?
750 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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