I am trying to create a filter drop down list to pass as parameter back to my controller. I cannot seem to get the drop down list to render.
Below is my model:
using System;
namespace ShiftPatternConfigurator.Models
{
public class ShiftModel
{
public int ShiftNo;
public string ShiftName;
public DateTime StartTime;
public DateTime FinishTime;
public string Team;
public int Week;
public int CycleWeek = 0;
public string StartDay;
public DateTime StartDate;
}
public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
}
Here's my Controller:
using Oracle.ManagedDataAccess.Client;
using ShiftPatternConfigurator.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
namespace ShiftPatternConfigurator.Controllers
{
public class HomeController : Controller
{
// list to hold the shift data
List<ShiftModel> shiftData = new List<ShiftModel>();
// GET: Index
public ActionResult Index()
{
ViewBag.Title = "Shift Pattern Configurator";
// create the select list to help pick which months shifts to look at
ViewBag.Month = new SelectList(Enum.GetValues(typeof(Month)),"Month", "Month", DateTime.Now.ToString("MMMM"));
// create the shift data to display in the page
return View(GetShiftData(shiftData, ViewBag.Month));
}
[HttpPost]
public ActionResult SelectMonth(SelectList Month)
{
ViewBag.Title = "Shift Pattern Configurator";
ViewBag.Month = Month;
return View(GetShiftData(shiftData, Month));
}
private List<ShiftModel> GetShiftData(List<ShiftModel> shiftData, SelectList monthIn)
{
// get the month enum from our selected value
Month month = (Month)Enum.Parse(typeof(Month), monthIn.SelectedValue.ToString());
DateTime start;
DateTime finish;
// if its the end of the year (December)
if ((int)month == 12)
{
// then show december and january
start = new DateTime(DateTime.Today.Year, (int)month, 1, 0, 0, 0);
finish = new DateTime(DateTime.Today.Year + 1, 2, 1, 0, 0, 0);
}
else
{
start = new DateTime(DateTime.Today.Year, (int)month, 1, 0, 0, 0);
finish = new DateTime(DateTime.Today.Year, (int)month + 1, 1, 0, 0, 0);
}
// build the query (Get shift records that are greater than or equal to today and 2 weeks worth)
StringBuilder oracleQuery = new StringBuilder("SELECT SHIFT_NO, SHIFT_NAME, START_TIME, FINISH_TIME, TEAM, WEEK, CYCLE_WEEK, START_DAY, START_DATE ");
oracleQuery.Append("FROM PROD_KPI.NEW_SHIFTS ");
oracleQuery.Append("WHERE START_DATE BETWEEN :pSTART_DATE AND :pEND_DATE ");
oracleQuery.Append("ORDER BY START_DATE DESC ");
// connection to oracle
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["IFSOracleConnection"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionString);
conn.Open();
// oracle command object and parameters
OracleCommand cmd = new OracleCommand(oracleQuery.ToString(), conn);
cmd.Parameters.Add(new OracleParameter("pSTART_DATE", OracleDbType.Date));
cmd.Parameters[0].Value = start;
cmd.Parameters.Add(new OracleParameter("pEND_DATE", OracleDbType.Date));
cmd.Parameters[1].Value = finish;
// execute the query
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//object o = reader.IsDBNull(0) ? null : reader.GetValue(0);
ShiftModel shiftRecord = new ShiftModel
{
ShiftNo = reader.GetInt32(reader.GetOrdinal("SHIFT_NO")),
ShiftName = reader.IsDBNull(reader.GetOrdinal("SHIFT_NAME")) ? null : reader.GetString(reader.GetOrdinal("SHIFT_NAME")),
StartTime = reader.GetDateTime(reader.GetOrdinal("START_TIME")),
FinishTime = reader.GetDateTime(reader.GetOrdinal("FINISH_TIME")),
Team = reader.IsDBNull(reader.GetOrdinal("TEAM")) ? null : reader.GetString(reader.GetOrdinal("TEAM")),
Week = reader.GetInt32(reader.GetOrdinal("WEEK")),
CycleWeek = reader.GetInt32(reader.GetOrdinal("CYCLE_WEEK")),
StartDay = reader.IsDBNull(reader.GetOrdinal("START_DAY")) ? null : reader.GetString(reader.GetOrdinal("START_DAY")),
StartDate = reader.GetDateTime(reader.GetOrdinal("START_DATE"))
};
shiftData.Add(shiftRecord);
}
reader.Close();
return shiftData;
}
}
}
Here's my Razor view:
@using ShiftPatternConfigurator.Models
@model IEnumerable<ShiftPatternConfigurator.Models.ShiftModel>
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("SelectMonth", "HomeController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.DropDownList("Month", (SelectList)ViewBag.Month)
<input type="submit" value="SelectMonth" />
}
<table class="table">
<tr>
<th>Shift No</th>
<th>Shift Name</th>
<th>Start Time</th>
<th>Finish Time</th>
<th>Team</th>
<th>Week</th>
<th>Start Day</th>
<th>Start Date</th>
<th></th>
</tr>
@if (Model.Count() > 0)
{
foreach (var item in Model)
{
<tr>
<td>@item.ShiftNo</td>
<td>@item.ShiftName</td>
<td>@item.StartTime</td>
<td>@item.FinishTime</td>
<td>@item.Team</td>
<td>@item.Week</td>
<td>@item.StartDay</td>
<td>@item.StartDate.ToShortDateString()</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
}
else
{
<tr>
<td colspan="10" align="center"><h2>No Data</h2></td>
</tr>
}
</table>
Can someone suggest how to create the DropDownList object so that when the form is submitted it passes the Month parameter back to my select month ActionResult in the HomeController.
When I run my code and break just prior to creating the DropDownList, if I inspect my ViewBag.Month I can see a populated SelectList with the Selected Value set to the current month.
Advice please.
EDIT: The Error I am receiving is the following:
System.Web.HttpException: 'DataBinding: 'ShiftPatternConfigurator.Models.Month' does not contain a property with the name 'Month'.'