1

我有以下模型:

    [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;}
    }

在提琴手我创建了以下请求:

    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

使用以下数据:

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

到 API URL: 类型: PUT: URL:http://localhost:8445/FinancialYear

当我看这里时:

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 };
            }
        }

仅处理提交的第一个日期,提交的第二个日期默认为 01/01/0001。我已经交换了开始日期和结束日期来测试这个:

例如:

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

正确填写模型上的 DateEnd,但是 DateStart = 01-01-0001

但是,如果我将数据更改为:

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

现在 DateStart 是正确的,但 DateEnd 现在是 01-01-0001

似乎在绑定到模型时,它只会让您拥有一个日期,因此会忽略或无法绑定第二个日期。

我没有任何错误,什么都没有。我还添加了以下项目:

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);

并且还尝试了带有斜线和所有内容的日期格式,而且我实际上并没有在这里发布 JSON,所以这不是 JSON 序列化问题,如果是,那么这个 json 是否会被创建,为什么它会做得如此糟糕它的工作。

4

1 回答 1

2

尝试放置以下有效负载:

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

注意我使用的日期的 ISO8601 格式。

于 2013-10-28T11:42:31.927 回答