Link to home
Create AccountLog in
Avatar of MSSC_support
MSSC_supportFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.NET list item not working

I am not a web developer but I have a good understanding of how certain things work but I do not understand this particular problem:

I have a site that has a dropdown list for picking dates. I want to add the dates 2001-2010 onto the code below but cannot seem to get it to function.


 <asp:DropDownList ID="ddlYear" runat="server" Height="21px" Width="56px">			
                        <asp:ListItem>Year</asp:ListItem>
                        <asp:ListItem>2000</asp:ListItem>
                        <asp:ListItem>1999</asp:ListItem>
                        <asp:ListItem>1998</asp:ListItem>
                        <asp:ListItem>1997</asp:ListItem>
                        <asp:ListItem>1996</asp:ListItem>
                        <asp:ListItem>1995</asp:ListItem>
                        <asp:ListItem>1994</asp:ListItem>
                        <asp:ListItem>1993</asp:ListItem>
                        <asp:ListItem>1992</asp:ListItem>
                        <asp:ListItem>1991</asp:ListItem>
                        <asp:ListItem>1990</asp:ListItem>
                        <asp:ListItem>1989</asp:ListItem>
                        <asp:ListItem>1988</asp:ListItem>
                        <asp:ListItem>1987</asp:ListItem>
                        <asp:ListItem>1986</asp:ListItem>
                        <asp:ListItem>1985</asp:ListItem>
                        <asp:ListItem>1984</asp:ListItem>
                        <asp:ListItem>1983</asp:ListItem>
                        <asp:ListItem>1982</asp:ListItem>
                        <asp:ListItem>1981</asp:ListItem>
                        <asp:ListItem>1980</asp:ListItem>
                        <asp:ListItem>1979</asp:ListItem>
                        <asp:ListItem>1978</asp:ListItem>
                        <asp:ListItem>1977</asp:ListItem>
                        <asp:ListItem>1976</asp:ListItem>
                        <asp:ListItem>1975</asp:ListItem>
                        <asp:ListItem>1974</asp:ListItem>
                        <asp:ListItem>1973</asp:ListItem>
                        <asp:ListItem>1972</asp:ListItem>
                        <asp:ListItem>1971</asp:ListItem>

Open in new window




I added the entry below to the asp.net file yet it does not appear. I have the source code and tried to rebuild the whole thing and it still does not show up. Can anyone help me?

 
  <asp:ListItem>2010</asp:ListItem>
                        <asp:ListItem>2009</asp:ListItem>
                        <asp:ListItem>2008</asp:ListItem>
                        <asp:ListItem>2007</asp:ListItem>
                        <asp:ListItem>2006</asp:ListItem>
                        <asp:ListItem>2005</asp:ListItem>
                        <asp:ListItem>2004</asp:ListItem>
                        <asp:ListItem>2003</asp:ListItem>
                        <asp:ListItem>2002</asp:ListItem>
                        <asp:ListItem>2001</asp:ListItem>

Open in new window

Avatar of kblau
kblau

Do this in MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EEDropdownDates.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            List<SelectListItem> yrs = new List<SelectListItem>();
            for (int i = 1971; i < 2011; i++)
            {
                var newItem = new SelectListItem { Text = i.ToString(), Value = i.ToString() };
                yrs.Add(newItem);
            }
           
            return View(yrs);
        }

    }
}

@model IEnumerable<SelectListItem>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.DropDownList("yrs",Model)
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of MSSC_support

ASKER

Thank you very much. This works as it should.