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

ASP.NET MVC3 RC2从请求参数绑定到方法参数的bug

发布时间:2020-12-16 09:47:02 所属栏目:asp.Net 来源:网络整理
导读:将值发布到控制器方法时遇到错误,其中一个参数是可为空的int. 重现步骤: 我已经创建了一个测试方法 [HttpPost]public ActionResult PostTest(string from,int? number,bool? formsearch){return new ContentResult { Content = string.Format("{0}/{1}/{2}"
将值发布到控制器方法时遇到错误,其中一个参数是可为空的int.
重现步骤:

我已经创建了一个测试方法

[HttpPost]
public ActionResult PostTest(string from,int? number,bool? formsearch)
{
return new ContentResult { Content = string.Format("{0}/{1}/{2}",from,number,formsearch) };
}

使用jquery,我创建一个Post请求

$.post("http://localhost/mysite/test/posttest",{ from:"1//1/2009",number:"156",formsearch:true});

请求(例如,在小提琴手中)清楚地显示了正在发送的值:

自:1 //二千○九分之一
数量:156
formsearch:真

但是从这个函数返回的结果是:

1 //二千○九分之一//真

如果我改变int? number到int number,结果是正确的:

1 // 1/2009/156 /真

在MVC3 RC1中,这对nullable int没有任何问题

更新:我似乎没有新创建的MVC3网站的问题.我的项目中有什么能影响模型绑定到可空int的?为什么RC1和RC2之间会有区别?有人有调试这个模型绑定问题的建议吗?

解决方法

这是MVC 3 RC2版本中的已知错误. ScottGu said:

we have seen a few reports of a metadata caching bug that manifests itself in at least two scenarios:

  • Nullable parameters in action methods have problems: When you have a controller action method with a nullable parameter (like int? – or a complex type that has a nullable sub-property),the nullable parameter might always end up being null – even when the request contains a valid value for the parameter.

我链接的博客文章包含一个解决方法:在Application_Start中添加一行:

// Workaround to fix RC2 bug with Metadata caching
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();

但实际上你应该更新到RTM.如果RC2的上线许可证在RTM版本之后幸存下来,我会感到非常惊讶.

(编辑:李大同)

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

    推荐文章
      热点阅读