Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-26
3
Medium Priority
?
517 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
[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
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 400 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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