Been banging my head off the wall for a couple of days with this one and can't seem to figure it out or find an article that answers my question.
I'm working on a rota system for work, which is almost complete, but I'm stuck on this part in the admin area. It will allow management to edit multiple rota entries for a given date range, for a given team. It will eventually use a view model to pass in the dateFrom, dateTo, and Team (teamID) - I've just done it this way for now to try to get it working.
The rotas are stored in the Rotas table (see below.) Rotas table
MorningShift and EveningShift (both FK's) store the employee's payroll number e.g. 123456.
The employee details are stored in the TeamMembers table (see below.) TeamMembers table
The Username in the TeamMembers table is the employee's payroll number.
My issue is that I can't seem to get the currently entered value in the rotas table to display in the dropdown list in the view. It just has the first employee in the select list (Model.TeamMembers) displayed for all dropdowns. My question is, how do I get the current value to display in each dropdown in the view? I did try
@Html.DropDownListFor(m => m.Rotas[i].MorningShift, Model.TeamMembers,
Model.Rotas[i].TeamMember1.FirstName + " " + Model.Rotas[i].TeamMember1.Surname, null)
but while this shows the correct employee, it doesn't have a value set for the option, and obviously shows the employee's name again because that is in the selectlist.
What I have so far:
The controller:
public async Task<ActionResult> ModifyRotas(DateTime? dateFrom,
DateTime? dateTo, int? Team)
{
Auth auth = new Auth();
bool isAdmin = auth.IsAdmin();
if (!isAdmin)
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
if (dateFrom != null && dateTo != null)
{
DateTime today = DateTime.Today;
DateTime startDate = Convert.ToDateTime(dateFrom);
DateTime endDate = Convert.ToDateTime(dateTo);
// Make sure earlier shifts can't be modified
if (startDate < today)
{
startDate = today;
}
// Verify the team exists
Team teamDetails = await db.Teams.FindAsync(Team);
if (teamDetails == null)
{
return HttpNotFound();
}
ModifyRotaVM vm = new ModifyRotaVM();
// Get rotas for the team for the dates specified
var rotas = await db.Rotas.Where(r => r.TeamID == Team && (r.ShiftDate >= startDate && r.ShiftDate <= endDate)).ToListAsync();
vm.Rotas = rotas;
// Generate the dropdown lists
var teamMembers = db.TeamMembers.Where(m => m.TeamID == Team && m.Deleted == 0).Select(m => new SelectListItem() { Text = m.FirstName + " " + m.Surname, Value = m.Username });
ViewBag.MorningShift = teamMembers;
ViewBag.EveningShift = teamMembers;
vm.TeamMembers = teamMembers;
return View(vm);
}
// If we got here, something went wrong; redisplay the form
return RedirectToAction("Modify");
}
The VM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TeamRota.Models
{
public class ModifyRotaVM
{
public List<Rota> Rotas { get; set; }
public IEnumerable<SelectListItem> TeamMembers { get; set; }
}
}
The view:
@model TeamRota.Models.ModifyRotaVM
@{
ViewBag.Title = "ModifyRotas";
}
<h2>Modify Rotas</h2>
@using (Html.BeginForm("RotaMod", "Rotas", new { area = "Admin" },
FormMethod.Post))
{
// Being passed in so we can redirect back to the same page after the update
@Html.Hidden("startDate", Request.QueryString["dateFrom"])
@Html.Hidden("endDate", Request.QueryString["dateFrom"])
@Html.Hidden("TeamID", Request.QueryString["Team"])
<table width="100%">
<tr>
<td>Shift date</td>
<td>Morning shift</td>
<td>Evening shift</td>
</tr>
@for (int i = 0; i < Model.Rotas.Count(); i++)
{
<tr>
<td>
@Html.HiddenFor(m => m.Rotas[i].ID)
@Html.DisplayFor(m => m.Rotas[i].ShiftDate)
</td>
<td>@Html.DropDownListFor(m => m.Rotas[i].MorningShift, Model.TeamMembers, Model.Rotas[i].TeamMember1.FirstName + " " + Model.Rotas[i].TeamMember1.Surname, null)</td>
<td>@Html.DropDownListFor(m => m.Rotas[i].EveningShift, Model.TeamMembers, Model.Rotas[i].TeamMember.FirstName + " " + Model.Rotas[i].TeamMember.Surname, null)</td>
</tr>
}
<tr>
<td colspan="4"><input type="submit" class="btn btn-success" value="Update" /></td>
</tr>
</table>
}
Would really appreciate any help that anyone can offer.
Thanks.
Tom