我有以下型号:
[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;}
}
在fiddler我创建了以下请求:
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网址:TYPE:PUT:网址: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是否会被创建,为什么它在做这样糟糕的工作。
答案 0 :(得分:2)
尝试PUTing以下有效负载:
Id=4&Name=Tester+2&DateEnd=2013-01-10&DateStart=2013-12-31&Enabled=true
请注意我使用的日期的ISO8601格式。