Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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