user2915962
user2915962

Reputation: 2711

List of datetimes in MVC-project

I have two dropdownlists in my project, one list with strings and one list with datetimes. My controller checks the first list (of strings) like this:

if (string.IsNullOrEmpty(animals))
    return View(person);
else
    return View(person.Where(d => d.animals == animals));

The same code wont work with the lists of datetimes because I get an error saying that a datetime cant be null. Any ideas of how i can make something similar with a list of datetimes?

enter image description here

Upvotes: 0

Views: 162

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31636

Have the view handle the null situation and the situation were this is a default min value in the list:

return View( ((MyListOfDates != null) && (MyListOfDates.Any()) ?
              MyListOfDates.FirstOrDefault(dtTime=> dtTime != DateTime.MinValue) :
              DateTime.MinValue);

Upvotes: 2

Related Questions