davethecoder
davethecoder

Reputation: 3932

C# WebApi only accepts one date field per model

I have the following model:

    [Alias("FinancialYears")]
    public class FinancialYear : BaseModel , IHasId<int> 
    {
        [Alias("Id")]
        [AutoIncrement]
        public int Id { get; set;}

        [StringLength(30, ErrorMessage = "Max Allowed Length has exceeded 30")]
        [Required(ErrorMessage = "Name is a required field")]
        public string Name { get; set;}

        [Display(Name="Start Date"), DataType(DataType.Date)]   
        [Required(ErrorMessage = "Start Date is a required field")]
        public DateTime DateStart { get; set;}

        [Display(Name = "End Date"), DataType(DataType.Date)]   
        [Required(ErrorMessage = "End Date is a required field")]
        public DateTime DateEnd { get; set;}

        [Display(Name="End Year")]   
        [Required(ErrorMessage = "End Year is a required field")]
        public bool Enabled { get; set;}
    }

in fiddler I have created the following request:

    Host: localhost:8445
Connection: keep-alive
Content-Length: 79
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://somedomain.com/FinancialYear/CreateOrEdit/4
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: __RequestVerificationToken=MvcSsMt6hSyiaKhTd0Z5z0O5VWsykA1oL4jOb-oMvmoFhtfk1yP-RqWYIaWPBkRyTtwRWvtZtdxunKvh2u5iTF58nozUUESNhd6UB48c-8E1; .ASPXAUTH=14755824C875E3D1B64014A2ACD0FD7A4860F004E03C89FDD55AD7638342487864E5A6DA7C8B42628FA9277030C7330D073FE0C0CD3A809688EC1342D397295DC4E05362E0F2616BB82A753150878A53B110B6558B7334BCE86184138540AE48; ASP.NET_SessionId=izglfvir3cjltz4q35eihm2a

With the following data:

Id=4&Name=Tester+2&DateEnd=10%2F01%2F2013&DateStart=31%2F12%2F2013&Enabled=true

to the API URL: TYPE: PUT: URL: http://localhost:8445/FinancialYear

when I look here:

public AjaxReturnModel Put(FinancialYear model)
        {
            try
            {
                var didPost = GlobalPut<FinancialYear>(model);
                return new AjaxReturnModel { Answer = didPost.Success ? "SUCCESS" : "FAIL", StringResponse = didPost.ExceptionMessage };
            }
            catch (Exception ex)
            {
                Common.CompileErrorMessage(ex, this.GetType().ToString());
                return new AjaxReturnModel { Answer = "FAIL", StringResponse = ex.Message };
            }
        }

only the first date filed is processed, the second date filed is defaulted to 01/01/0001. I have swapped around the start date and end date to test this:

for example:

Id=4&Name=Tester+2&DateEnd=10%2F01%2F2013&DateStart=31%2F12%2F2013&Enabled=true

Fills in the DateEnd on model correctly, however DateStart = 01-01-0001

However if i change the data to:

Id=4&Name=Tester+2&DateStart=10%2F01%2F2013&DateEnd=31%2F12%2F2013&Enabled=true

Now DateStart is correct, but DateEnd is now 01-01-0001

It seems that when binding to the model, it will only allow you to have one date, and will therfore ignore, or fail to bind the second date.

I have no errors from this, nothing at all. I have also added the following items:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "dd-MM-yyyy" });
        GlobalConfiguration.Configuration.Services.RemoveAll(typeof(System.Web.Http.Validation.ModelValidatorProvider),v => v is InvalidModelValidatorProvider);

and also tried date format with slashes and everything, Also I am not actually posting JSON here, so this is not a JSON serialization problem, and If it is, then were would this json be getting created, and why is it doing such a terrible job of it.

Upvotes: 1

Views: 470

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

Try PUTing the following payload:

Id=4&Name=Tester+2&DateEnd=2013-01-10&DateStart=2013-12-31&Enabled=true

Notice the ISO8601 format of the dates I used.

Upvotes: 2

Related Questions