DataAnnotation的必需属性 [英] DataAnnotation for Required property

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

问题描述

首先,它的工作原理,但今天它失败!

这是我如何定义日期属性:

  [显示(名称=日期)]
[必需(的ErrorMessage =提交的日期是必需的。)
[DisplayFormat(DataFormatString ={0:D},ApplyFormatInEditMode = TRUE)]
[数据类型(DataType.Date)
公众的DateTime TripDate {搞定;组; }

有在过去一直在努力。但是今天,当我打电话一样ApiController动作:

  [HttpPost]
公众的Htt presponseMessage SaveNewReport(TripLeaderReportInputModel模型)

在Firebug的报告:

  ExceptionMessage:上键入属性TripDate'Whitewater.ViewModels.Report.TripLeaderReportInputModel
是无效的。值类型属性标记为[必填]也必须与被标记
[数据成员(IsRequired =真)]的要求得到认可。考虑的归属
声明使用[DataContract]类型和财产[数据成员(IsRequired =真)。ExceptionTypeSystem.InvalidOperationException

发生了什么?是不是那些 [DataContract] WCF ?我在 MVC4

使用 REST的WebAPI

谁能帮助?好吗?

--- ---更新

有我已经找到了一些类似的联系。

<一个href=\"http://forums.asp.net/t/1834524.aspx/2/10?Re%20MvC%204%200%20RTM%20broke%20us%20and%20we%20don%20t%20know%20how%20to%20fix%20it\">MvC 4.0 RTM打破了我们,我们不知道如何解决它的RSS

---再次更新---

下面是HTTP响应头:

 缓存控制无缓存
连接关闭
内容长度1846
内容类型应用程序/ JSON的;字符集= UTF-8
日期星期四,2012年9月6日17时48分十五秒GMT
过期-1
杂无缓存
服务器ASP.NET开发服务器/ 10.0.0.0
的X ASPNET-版本4.0.30319

请求头:

 接受* / *
接受编码gzip的,放气
接受语言EN-US,连接; Q = 0.5
缓存控制无缓存
连接保持
内容长度380
内容类型application / x-WWW的形式urlen codeD;字符集= UTF-8
曲奇饼.ASPXAUTH=1FF35BD017B199BE629A2408B2A3DFCD4625F9E75D0C58BBD0D128D18FFDB8DA3CDCB484C80176A74C79BB001A20201C6FB9B566FEE09B1CF1D8EA128A67FCA6ABCE53BB7D80B634A407F9CE2BE436BDE3DCDC2C3E33AAA2B4670A0F04DAD13A57A7ABF600FA80C417B67C53BE3F4D0EACE5EB125BD832037E392D4ED4242CF6
DNT 1
主机本地主机:39019
杂无缓存
Referer的HTTP://本地主机:39019 /报告/ TripLeader
用户代理的Mozilla / 5.0(Windows NT的6.1; WOW64; RV:15.0)的Gecko / 20100101火狐/ 15.0
的X要求,随着XMLHtt prequest

--- ---更新

我已经找到了一个临时解决方案。见下面回答。如果有人明白为什么它的工作原理,或有更好的解决方案,请发表你的答案。谢谢你。


解决方案

好吧。虽然我还没有完全理解这个事情。一种解决方法是找到。

的Global.asax

  GlobalConfiguration.Configuration.Services.RemoveAll(
    typeof运算(System.Web.Http.Validation.ModelValidatorProvider)
    V =&GT; v是InvalidModelValidatorProvider);

我发现它在aspnetwebstack问题跟踪。这里是链接到页面:

申请[数据成员(IsRequired = TRUE)]与值类型 <需要的属性过于积极的验证/ p>

如果任何人都可以告诉我们,为什么是这样的,请发表您的见解作为答复。谢谢你。

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

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

[HttpPost]
public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model)

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"

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

Can anyone help? please?

---update---

There are some similar links I have found.

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

--- update again ---

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

Request Header:

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

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.

In Global.asax:

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

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

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

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

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

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