I'll try to keep this short. I am new to MVC.
I have a Controller which creates a list of timeslots (like a schedule), and puts this into a ViewData. I understand how to do this.
I know how to display this data in a drop down menu using @Html.DropDownList
, for example my create page has the following:
@Html.DropDownList("TimeSlot", ViewData["TimeSlotsList"] as List<SelectListItem>)
@Html.ValidationMessageFor(model => model.TimeSlot)
My problem is on my index view, it displays the modelItem => item.TimeSlot
and shows the value of the Time slot, (1, 2, 3, 4, etc)
@Html.DisplayFor(modelItem => item.TimeSlot)
I want to display the TEXT value of the index from the ViewData[TimeSlotsList]
list. Obviously the index will be the value of the modelItem => item.TimeSlot
.
I know this would have been much easier if the TimeSlots was its own table in the database but it was not setup this way and at this point in time I can't change without causing myself all kinds of migration headaches, etc.
If anyone knows how to do this or can point me in the right direction it is hugely appreciated! I tried some different ideas but nothing has worked yet.....
EDIT:
I Create the list of timeslots in my controller with this function:
private List<SelectListItem> CreateTimeSlotsList()
{
//Build Time Slot Drop Down
List<SelectListItem> li1 = new List<SelectListItem>();
li1.Add(new SelectListItem { Text = "10-10:50am, Tues Mar. 17", Value = "1" });
li1.Add(new SelectListItem { Text = "11-11:50am, Tues Mar. 17", Value = "2" });
li1.Add(new SelectListItem { Text = "1:10-2pm, Tues Mar. 17", Value = "3" });
li1.Add(new SelectListItem { Text = "2:10-3pm, Tues Mar. 17", Value = "4" });
li1.Add(new SelectListItem { Text = "3:30-4:20pm, Tues Mar. 17", Value = "5" });
li1.Add(new SelectListItem { Text = "8:30-9:20am, Wed Mar. 18", Value = "6" });
li1.Add(new SelectListItem { Text = "9:30-10:20am, Wed Mar. 18", Value = "7" });
li1.Add(new SelectListItem { Text = "11-11:50am, Wed Mar. 18", Value = "8" });
li1.Add(new SelectListItem { Text = "1:10-2pm, Wed Mar. 18", Value = "9" });
li1.Add(new SelectListItem { Text = "2:10-3pm, Wed Mar. 18", Value = "10" });
li1.Add(new SelectListItem { Text = "Hands On/Demo", Value = "99" });
ViewData["timeSlots"] = li1;
return (li1);
}
Then i use
ViewData["TimeSlotsList"] = CreateTimeSlots();
I want to display the TEXT value of the index of the above list.
This isn't pretty, but:
TimeSlotList
;TimeSlotList
in ViewData
on the Index page;... then you could try something like this:
@Html.DisplayFor( modelItem =>
((List<SelectListItem>) ViewData["TimeSlotsList"])
.Single(sli => sli.Value==modelItem.TimeSlot)
.Text)
ETA: But you know what I'd do? I'd create a model for a TimeSlot
object, populate a master list of them the way you're doing in your CreateTimeSlots
function, and return that from your data layer the way you would the model objects you're getting. Basically, simulate a master TimeSlot
table.
Then I'd use that to
TimeSlotsList
, and ViewModel
based on your model object that includes a TimeSlot
object from your master list. Then, you could do something like:
@Html.DisplayFor(modelItem => item.TimeSlot.TimeSlotDescription)
... on your Index page.