Solved

How do I create a view model for a drop down list in ASP.NET MVC3?

Posted on 2012-03-26
3
494 Views
Last Modified: 2012-06-01
Hi,

I'm trying to teach myself MVC3. Im converting from webforms.

I need to create a view model that includes a dropdown list that I can pass to the controller and eventually render in the View.

How can I accomplish this? I have the skeleton but I dont know what the code is to actually create the name value pairs for the view.

namespace DH.ViewModels
{
    public class SharedLayoutViewModel
    {
        public IEnumerable<SelectListItem> Products
        {
            //something goes here to setup the value / name pairs to be rendered in the view.
            //example:
            //<option value="1">Mustard</option>
            //<option value="2">Ketchup</option>
            //<option value="3">Mayo</option>
            //<option value="4">Relish</option>
            //<option value="5">BBQ</option>
        }
    }
}

Thanks
0
Comment
Question by:maddhacker24
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 100 total points
ID: 37771440
1) Creating a new instance of a SelectListItem would be like this:

SelectListItem item = new SelectListItem 
{
    Text = product.Name,
    Value = product.ID.ToString()
};

Open in new window


2) It might not have to be IEnumerable<SelectListItem>, you could use List<SelectListItem>

List<SelectListItem> selectList = new List<SelectListItem>
{
    new SelectListItem 
   {
       Text = product.Name,
       Value = product.ID.ToString()
   };
}

return selectList;

Open in new window

0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38035803
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

19 Experts available now in Live!

Get 1:1 Help Now