Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:12:50+00:00 2026-05-31T20:12:50+00:00

I’m trying to teach myself MVC3. Im converting from webforms. I need to create

  • 0

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
        {
            //some code 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:12:52+00:00Added an answer on May 31, 2026 at 8:12 pm

    I would create a class for Product and the create Products Property in my main viewmodel which is of type List

    public class ProductViewModel
    {
      public int ID { set;get;}
      public string Name { set;get;}
    }
    
    public class OrderViewModel
    {
      public int OrderNumber { set;get;}
      public List<ProductViewModel> Products { set;get;}
      public int SelectedProductId { set;get;}    
    }
    

    and in your Controller Action method

    public ActionResult Order()
    {     
       var orderVM=new OrderViewModel();
       //Items hard coded for demo. You may replace with values from your db
       orderVM.Products= new List<ProductViewModel>
        {
            new ProductViewModel{ ID=1, Name="IPhone" },
            new ProductViewModel{ ID=2, Name="MacBook Pro" },
            new ProductViewModel{ ID=3, Name="iPod" }           
        };
       return View(orderVM);
    }
    

    and in your view which is strongly typed to OrderViewModel.

    @model ORderViewModel
    @using (Html.BeginForm())
    {
      <p> 
        @Html.DropDownListFor(x => x.SelectedProductId ,
                         new SelectList(Model.Products, "ID", "Name"), "-- Select Product--")
      </p>
      <input type="submit" />
    }
    

    I have added a SelectedProductId property also, so you will get the user selected value from the dropdown in that Property when user post the form back to the controller.

    You can also use the generic SelectListItem type collection as your view model property to transfer the dropdown data instead of your custom ProductViewModel collection.

    public class OrderViewModel
    {
      public int OrderNumber { set;get;}
      public List<SelectListItem> Products { set;get;}
      public int SelectedProductId { set;get;}    
    }
    

    and in the GET action,

    public ActionResult Order()
    {     
       var orderVM=new OrderViewModel();
       //Items hard coded for demo. You may replace with values from your db
       orderVM.Products= new List<SelectListItem>
        {
            new SelectListItem {Value = "1", Text = "IPhone"},
            new SelectListItem {Value = "2", Text = "MacBook"},
            new SelectListItem {Value = "3", Text = "Candy"}
        };
       return View(orderVM);
    }
    

    And in your view,

    @Html.DropDownListFor(x => x.SelectedProductId, Model.Products, "-- Select Product--")
    

    EDIT : As per the request from OP, edited the answer to have the Property returning static items as products

    I added a get implementation to Products property to return a list of static products.

    public class OrderViewModel
    {
            private List<ProductViewModel> _products;
            public int OrderNumber { set; get; }
            public List<ProductViewModel> Products
            {
                get
                {
                    if (_products == null)
                    {
                        _products = new List<ProductViewModel>();
                        _products.Add(new ProductViewModel { ID = 1, Name = "Ketchup" });
                        _products.Add(new ProductViewModel { ID = 1, Name = "Mustard" });
                        _products.Add(new ProductViewModel { ID = 1, Name = "Relish" });
                        _products.Add(new ProductViewModel { ID = 1, Name = "Mayo" });
                    }
                    return _products;
                }
            }
           public int SelectedProductId { set;get;}
    }
    

    Now in your controller, you don’t need to call the GetAvailableProductsmethod as it is already there. So the controller will looks like this.

        public ActionResult Order()
        {
            OrderViewModel orderVM = new OrderViewModel();           
            return View(orderVM);
        }
    

    Here is the output.
    enter image description here

    If you have many items in the products, move it to a method and call that method int he get implementation instead of writing that there. That is much cleaner approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.