I have a project that queries a set of times from a database and uses these entries to populate a dropdownlist. Once the page is loaded, the user can select one of the items in the DropDownList. When the "Save" button is clicked, the Selected item value is returned to the [HttpPost] version of the controller action. This item is then stored into session. If the system returns to the page containing the dropdown, I want the saved Value to be selected in the DropDownList. What actually happens is that the DropDownList is always set to be the first item in the list.

Database Table: This data has been imported using Link to SQL



     TestTimeID              Int         Identity(1,1)   NOT NULL   PRIMARY KEY,
     ResourceTime            VARCHAR(12)                 NOT NULL   DEFAULT null ,

     Link2ClientLocationID   Int                         NOT NULL

                FOREIGN KEY REWFERENCES ClientLocation(ClientLocationID)



public IQueryable<TestTime> FindTimes(int intLocationID)


     return from TestTime in db.TestTimes

                where (TestTime.Link2ClientLocationID == intLocationID)

                select TestTime;



public class EventTimeEntry


     public int TimeID { get; set; }

     public string EventTime { get; set; }

     public IEnumerable<SelectListItem> AvailableTimes { get; set; }



public ActionResult EventTime()


     EventTimeEntry model;

     if (SessionWrapper.ASessionUser.sesEventTimeEntry == null)


          model = new EventTimeEntry();

          model.AvailableTimes = new SelectList(repository.FindTimes(SessionWrapper.ASessionUser.LocationID).ToList(), "TestTimeID", "ResourceTime");




          model = SessionWrapper.ASessionUser.sesEventTimeEntry;

// Below line is added to provide a value for testing the selected item when the view no longer sends the selected value to the [HttpPost] controller action

// SessionWrapper.ASessionUser.sesSelectedTime = 11;

          model.AvailableTimes = new SelectList(repository.FindTimes(SessionWrapper.ASessionUser.LocationID).ToList(), "TestTimeID", "ResourceTime", SessionWrapper.ASessionUser.sesSelectedTime ); 


     SessionWrapper.ASessionUser.sesEventTimeEntry = model;

     return View(model);





public ActionResult EventTime(EventTimeEntry entry)


     SessionWrapper.ASessionUser.sesSelectedTime = entry.TimeID;

     varNextPage = repository.GetNextPage(SessionWrapper.ASessionUser.CurrentPage, SessionWrapper.ASessionUser.LocationID);

     string strNextPage = String.Join("", NextPage);

     return RedirectToAction(strNextPage);



<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<OER.Models.EventTimeEntry>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

     <h2>Event Registration</h2>

     <p>Please select the time for your event</p>

     <% using (Html.BeginForm())

        { %>


                 <legend>Event Time</legend>

                 <%= Html.LabelFor(model => model.EventTime) %>

                 <br />

                 <%: Html.DropDownList("TimeID", Model.AvailableTimes)%>


            <br />


                 <input type="submit" value="SaveToSessionEventTime" />


        <% } %>


From searching online I found that by changing the name of the DropDownList in the View to "fredTimeID", and hardcoding a value for the Selected value in the controller event to a know value in the items, that the DropDownList DOES appear with the correct selected value. But by doing that, the Selected item from the DropDownList is no longer being sent to the [HttpPost] controller action.

It appears that I can either select an item in the DropDownList using the last parameter of the "SelectList" function OR return the selected value from the DropDownList to the [HttpPost] controller action.

This should be a common problem but I cant find the solution. Any help is greatly appreciated.

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

7 Years
Discussion Span
Last Post by melton16

Where is MVC2? (Marvel vs. Capcom 2)

I will read your code after it has been tagged and try and offer a solution. :)


Ok instead of setting default in the back set it in the front,

On Page Load do

DropDownList1.SelectedIndex = 0;

Where are you saving to Database?


hi melton16,

you need to write your selectlist again in

public ActionResult EventTime(EventTimeEntry entry) {

Lets see an example I want to add user

public ActionResult UserRegistration()
            ViewData["txtUserCity"] = new SelectList(_db.City.ToList(), "CityName", "CityName");
            ViewData["txtUserState"] = new SelectList(_db.State.ToList(), "StateName", "StateName");
            return View();
        //User Registration After form submission
        public ActionResult UserRegistration(string txtUserCity, string txtUserState,[Bind(Exclude ="UserId")] User u)

            ViewData["txtUserCity"] = new SelectList(_db.City.ToList(), "CityName", "CityName");
            ViewData["txtUserState"] = new SelectList(_db.State.ToList(), "StateName", "StateName");

here I am populating dropdownlist txtUserCity and txtUserState.

In next ActionResult of UserRegistartion (when form submitted)
the item which you hae selected will came into string txtUserCity , no need to generate session for that.

try it! it may help you!
If any error occured please ask!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.