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
Solved

@Html.DropDownListFor -- ASP.net MVC4

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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