加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – DataAnnotation的必需属性

发布时间:2020-12-15 19:28:09 所属栏目:asp.Net 来源:网络整理
导读:首先它工作,但今天它失败了! 这是我如何定义date属性: [Display(Name = "Date")][Required(ErrorMessage = "Date of Submission is required.")] [DisplayFormat(DataFormatString = "{0:d}",ApplyFormatInEditMode = true)][DataType(DataType.Date)]pub
首先它工作,但今天它失败了!

这是我如何定义date属性:

[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操作:

[HttpPost]
public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model)

Firebug报告:

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!

任何人可以帮助?请?

— update —

有一些类似的链接,我发现。

MvC 4.0 RTM broke us and we don’t know how to fix it RSS

— update again —

这里是HTTP响应头:

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

— update —

我找到了一个临时解决方案。参见下面的答案。如果有人明白为什么它工作或有更好的解决方案,请发布您的答案。谢谢。

解决方法

好的。虽然我还没有完全理解这件事。找到解决方法。

在Global.asax:

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

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

Overly aggressive validation for applying [DataMember(IsRequired=true)] to required properties with value types

如果任何人可以告诉我们为什么是这样的,请张贴你的见解作为答案。谢谢。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读