必需属性的数据注释 [英] DataAnnotation for Required property

查看:24
本文介绍了必需属性的数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一开始它有效,但今天它失败了!

First it works, but today it failed!

这就是我定义日期属性的方式:

This is how I define the date property:

[Display(Name = "Date")]
[Required(ErrorMessage = "Date of Submission is required.")]        
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime TripDate { get; set; }

它过去一直有效.但是今天,当我调用相同的 ApiController 操作时:

It has been working in the past. But today, when I call the same ApiController action:

[HttpPost]
public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model)

Firebug 报告:

The Firebug reports:

ExceptionMessage:

"Property 'TripDate' on type 'Whitewater.ViewModels.Report.TripLeaderReportInputModel' 
is invalid. Value-typed properties marked as [Required] must also be marked with
[DataMember(IsRequired=true)] to be recognized as required. Consider attributing the 
declaring type with [DataContract] and the property with [DataMember(IsRequired=true)]."

ExceptionType

"System.InvalidOperationException"

发生了什么?那些不是 [DataContract]WCF 吗?我在 MVC4 中使用 REST WebAPI

What happened? Isn't those [DataContract] for WCF? I am using the REST WebAPI in MVC4!

有人可以帮忙吗?好吗?

Can anyone help? please?

---更新---

我发现了一些类似的链接.

There are some similar links I have found.

MvC 4.0 RTM 破坏了我们,我们不知道如何修复它 RSS

---再次更新---

这里是 HTTP 响应头:

Here is the HTTP Response Header:

Cache-Control   no-cache
Connection  Close
Content-Length  1846
Content-Type    application/json; charset=utf-8
Date            Thu, 06 Sep 2012 17:48:15 GMT
Expires         -1
Pragma          no-cache
Server          ASP.NET Development Server/10.0.0.0
X-AspNet-Version    4.0.30319

请求头:

Accept          */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control   no-cache
Connection          keep-alive
Content-Length  380
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie          .ASPXAUTH=1FF35BD017B199BE629A2408B2A3DFCD4625F9E75D0C58BBD0D128D18FFDB8DA3CDCB484C80176A74C79BB001A20201C6FB9B566FEE09B1CF1D8EA128A67FCA6ABCE53BB7D80B634A407F9CE2BE436BDE3DCDC2C3E33AAA2B4670A0F04DAD13A57A7ABF600FA80C417B67C53BE3F4D0EACE5EB125BD832037E392D4ED4242CF6
DNT                 1
Host            localhost:39019
Pragma          no-cache
Referer         http://localhost:39019/Report/TripLeader
User-Agent          Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0
X-Requested-With    XMLHttpRequest

--- 更新---

我找到了一个临时解决方案.请参阅下面的答案.如果有人了解它的工作原理或有更好的解决方案,请发布您的答案.谢谢.

I have found out a makeshift solution. See answer below. If anyone understand why it works or has better solutions, please post your answers. Thank you.

推荐答案

好的.虽然我还没有完全理解这个东西.找到了解决方法.

Okay. Though I have not complete understood this thing. A workaround is found.

Global.asax 中:

GlobalConfiguration.Configuration.Services.RemoveAll(
    typeof(System.Web.Http.Validation.ModelValidatorProvider),
    v => v is InvalidModelValidatorProvider);

我在 aspnetwebstack 的问题跟踪器中找到了它.这是页面的链接:

I found it in the Issue Tracker in aspnetwebstack. Here is the link to the page:

将 [DataMember(IsRequired=true)] 应用于具有值类型的必需属性的过于激进的验证

如果有人能告诉我们为什么会这样,请将您的见解作为答案发布.谢谢.

If anyone can tell us why it is like this, please post your insight as answers. Thank you.

这篇关于必需属性的数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆