Go Premium for a chance to win a PS4. Enter to Win

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
?
525 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
2 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

782 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