1º: Change your method to DropDownListFor, the signature has an extra parameter for the SelectList, it is the possible values to show on control.
2º: Build the select list using this extension method on enum:
public static SelectList ToSelectList<TEnum>(this TEnum enumObj,
Func<TEnum, string> getText = null,
Func<TEnum, object> getValue = null,
Func<TEnum, bool> ignore = null)
{
var query = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
if (ignore != null)
query = query.Where(e => !ignore(e));
var values = query.Select(e =>
{
object value = getValue != null ? getValue(e) : e;
string name = null;
if (getText != null)
name = getText(e);
name = name ?? EnumExtension.GetDisplayName((Enum)(object) e) ?? e.ToString();
return new { @Name = name, @Value = value };
});
return new SelectList(values, "Value", "Name", enumObj);
}
3º: The last optional parameter let you write an filter to ignore these values.
4º: You have to get Display Attribute from the EnumValue, use an static method like these: How to get the Display Name Attribute of an Enum member via MVC razor code?.
5º: For the Last, you have to parse string of display to DateTime and compare it to DateTime.UtcNow.
So the code (dense form, can be refactored and change use of Parse to TryParse) should be like that:
@Html.DropDownListFor(m => m.StartTime,
YogaTime.ToSelectList(t =>
DateTime.Parse(t.GetDisplayName()) <=
DateTime.UtcNow)),
new { @class = "form-control selectpicker" })
Extra: It's the way that you want, now I would like to suggest to don't build this at the .cshtml
You can do the same thing, but building the SelectList on the Controller and using the ViewBag, like that:
@Html.DropDownListFor(m => m.StartTime,
(SelectList)ViewBag.Times,
new { @class = "form-control selectpicker" })
Where ViewBag is the ExpandoObject for dynamic operations on view and can be build using the same methods that I mentioned or explicited before.
Hope it Helps!