Solved

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

Posted on 2012-03-26
3
497 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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