1

I have model with id and date. Then I make it angular.toJson and send to server. The problem is in that: if my date is empty, model's date field on server shows as {1/1/0001 12:00:00 AM} but not null. How to solve this? I need null.

Here is angularjs controller where my function calls

$scope.filterModel = function (filter) {
    postService.getFilteredByUserId($scope.bindModel, angular.toJson(filter));
};

Here is my server function that gets this model

[HttpPost]
public IEnumerable<TimesheetModel> FilterModel(TimesheetFilterModel filter)
{
    return TimesheetService.FilterTimesheetListByFilter(filter);
}

Here is model:

public class TimesheetFilterModel
{
    public int      UserId { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public int      TicketType { get; set; }
}
HUSTLIN
  • 196
  • 4
  • 17

2 Answers2

3
public class TimesheetFilterModel
{
    public int      UserId { get; set; }
    public DateTime? DateFrom { get; set; }
    public DateTime? DateTo { get; set; }
    public int      TicketType { get; set; }

    public TimesheetFilterModel() {
        DateFrom = null;
        DateTo = null;
    }
}

by default, DateTime is not nullable. You need to make them nullable and set them to null on Constructor.

Sefa
  • 8,865
  • 10
  • 51
  • 82
  • 1
    You do not need to set `null` in a constructor (the default is `null`) –  Sep 29 '15 at 08:13
1

You can make the datetime property nullable so that on getting null value it will handle the null.

DateTime is a value type in c# so assigning null value in it not possible directly.

DateTime? nullableDate;
dateSample.Value = null;

In your case it's going to default to DateTime.MinValue.