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
CREATE TABLE dbo.TestTime
(
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)
) ON [PRIMARY]
Repository:
public IQueryable<TestTime> FindTimes(int intLocationID)
{
return from TestTime in db.TestTimes
where (TestTime.Link2ClientLocationID == intLocationID)
select TestTime;
}
ViewModel:
public class EventTimeEntry
{
public int TimeID { get; set; }
public string EventTime { get; set; }
public IEnumerable<SelectListItem> AvailableTimes { get; set; }
}
Controller:
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");
}
else
{
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);
}
[HttpPost]
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);
}
View:
<%@ 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">
EventTime
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Event Registration</h2>
<p>Please select the time for your event</p>
<% using (Html.BeginForm())
{ %>
<fieldset>
<legend>Event Time</legend>
<%= Html.LabelFor(model => model.EventTime) %>
<br />
<%: Html.DropDownList("TimeID", Model.AvailableTimes)%>
</fieldset>
<br />
<div>
<input type="submit" value="SaveToSessionEventTime" />
</div>
<% } %>
</asp:Content>
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.