0

On my page I have a form for filtering a list of machines. The form posts with GET method. Two of the fields in that form are text fields for entering a date.

Based on the inputs in the form, I'm filtering the list. My problem is, that the date filtering doesn't work as expected.

The dates are entered in the format dd/MM/yyyy. The system CultureInfo is "da-DK". Dates are stored i MSSQL as yyyy-MM-dd.

I have this filter model:

public class MachineFilter
{

    [DisplayName("Search")]
    public string Search { get; set; }

    [DisplayName("Installation date, from")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? InstallationDateFrom { get; set; }

    [DisplayName("Installation date, to")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? InstallationDateTo { get; set; }

}

And the filtering (simplified):

[BindProperty(SupportsGet = true)]
public MachineFilter MachineFilter { get; set; }

----

Machines = await _dbContext.Machines.ToListAsync();

if (MachineFilter.InstallationDateFrom != null)
{
    Machines = Machines.Where(x => x.InstallationDate != null && x.InstallationDate >= MachineFilter.InstallationDateFrom).ToList();
}

In this example two things can happen: 1) the filtering isn't applied - or 2) wrong results returns to the list.

From this SO question I also tried converting the input to string and back to DateTime with DateTime.ParseExact:

string dateFrom = MachineFilter.InstallationDateFrom?.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
DateTime dFrom = DateTime.ParseExact(dateFrom, "yyyyMMdd", CultureInfo.InvariantCulture);
Machines = Machines.Where(x => x.InstallationDate != null && x.InstallationDate >= dFrom).ToList();

But no matter what I do, I end up with a wrong DateTime format, so entering 01/12/2019 (dd/MM/yyyy) becomes 01/12/2019 (MM/dd/yyyy).

Right now the culture is only "da-DK", so I could do some splitting of the posted input and add a new DateTime(yyyy, MM, dd) of the array, but soon the system will scale to other countries and then dates will be entered as MM/dd/yyyy, so I'd like a more "build-in" approach if possible.

Thanks.

6
  • If this is from a web-page, then you can use the standard javascript iso date-format with standard MVC date binders, as per my answer here: stackoverflow.com/questions/10699851/… You should also postpone the .ToListAsync() to after the .Where - statements. I can put this in an answer, if you think I understood the situation correct. Commented Jan 31, 2020 at 12:35
  • I'm not sure what you mean. There's no javascript in this part. Commented Jan 31, 2020 at 12:41
  • What's your front-end? An MVC razor page? Commented Jan 31, 2020 at 12:47
  • Yes, .NET Core with Razor pages. Commented Jan 31, 2020 at 12:51
  • 1
    In believe I've solved it myself. In my filter model I changed InstalledFromDate/InstalledToDate to string and just do DateTime.Parse on the string posted. Sorry for taking your time. Commented Jan 31, 2020 at 13:10

1 Answer 1

0

The solutions to my problem was to set the model types to string instead of DateTime and in my filter parse them to DateTime, like:

[DisplayName("Installation date, from")]
public string InstallationDateFrom { get; set; }

[DisplayName("Installation date, to")]
public string InstallationDateTo { get; set; }

--

Machines = Machines.Where(x => x.InstallationDate != null && x.InstallationDate >= DateTime.Parse(MachineFilter.InstallationDateFrom)).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.