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 7749383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:02:11+00:00 2026-06-01T11:02:11+00:00

I am just trying to populate a html.dropdown list using mvc2 in VS2008. But

  • 0

I am just trying to populate a html.dropdown list using mvc2 in VS2008.

But the control is not displayed at all.

Here is my code

public ActionResult Index()
{

    ViewData["Time"] = DateTime.Now.ToString();
    var mdl = new List<SelectListItem>();

    mdl.Add(new SelectListItem
    {
        Value = "1",
        Text = "Module One"
    });
    mdl.Add(new SelectListItem
    {
        Value = "2",
        Text = "Module Two"
    });
    ViewData["moduleList"] = new SelectList(mdl,"Value", "Text");

    return View("MainMenu");
}

and here is the markup

<div>
  <%Html.DropDownList("moduleList", (IEnumerable<SelectListItem>)ViewData["moduleList"]); %>
</div>

Where did i go wrong ?

  • 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-06-01T11:02:13+00:00Added an answer on June 1, 2026 at 11:02 am

    You are best putting that stuff in your model so for example

    in the controller

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
    
            ViewData["Time"] = DateTime.Now.ToString(CultureInfo.InvariantCulture);
            var mdl = new List<SelectListItem>
                          {
                              new SelectListItem
                                  {
                                      Value = "1",
                                      Text = "Module One"
                                  },
                              new SelectListItem
                                  {
                                      Value = "2",
                                      Text = "Module Two"
                                  }
                          };
    
            ViewData["moduleList"] = new SelectList(mdl, "Value", "Text");
    
            var model = new HomeModel
                            {
                                SelectedItem = 1,
                                items = mdl
                            };
            return View(model);
        }
    

    }

    Now create the model

    namespace MvcApplication1.Models
    {
        public class HomeModel
        {
            public int SelectedItem { get; set; }
            public IEnumerable<SelectListItem> items { get; set; }
        }
    }
    

    your page will look like this on a test site

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.HomeModel>" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Home Page
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <h2><%: ViewData["Message"] %></h2>
        <p>
            To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
        </p>
        <div> 
      <%=Html.DropDownList("SelectedItem", Model.items)%>
        </div> 
    </asp:Content>
    

    Now an explanation, you have created a model for the view and this model is returned to the page by the controller the page is inheriting from a ViewPage which takes the generic argument of the model that was supplied to it by the controller

    The markup is saying “give me a html drop down and mark the selected item as the first selected, the items come from the model (which is what your controller supplied it).

    In the real world the data would come from your data layer and not directly in the controller (I like as little code in the controller as possible)

    edit:

    You have a typo for your example try this

    <%= Html.DropDownList("moduleList") %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate an HTML table with MySQL data, but I'm getting an
Ok so here is my problem. I'm trying to populate an @Html.DropDownListFor() with my
Just trying out this IDE but it already looks better than NetBeans in terms
Just trying to get my irb sessions to actually list the current line of
just trying to test for equality in this piece of code, but getting a
I was trying out this code for getting a dropdown list populated by a
Just starting out using MVC3 and hit a problem trying to build a drop-down
I'm trying to populate a div in html via ajax. The data source is
I just started learning php last week and I am trying to populate values
Am new to this and just trying to submit dynamically populated select list on

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.