Since ASP.NET MVC3 RC2 I encounter a bug when posting values to a controller method of which one of the parameter is a nullable int.
Steps to reproduce:
the request (eg. in fiddler) shows clearly the values are being sent:
from:1//1/2009
number:156
formsearch:true
but the result returned from this function is:
1//1/2009//True
If I change int? number to int number, the result is correct:
1//1/2009/156/True
In MVC3 RC1 this was working without any problems with the nullable int
Update: I don't seem to have the problem with a newly created MVC3 website. What could I have in my project that influence model binding to nullable int's? And why would there be a difference between RC1 and RC2? Anyone have suggestions for debugging
this model binding problem?
Could you do a diff of your two projects and see what's different. I can't think of anything that would affect this, but maybe something will jump out?
Quite difficult to do, because one is a rather large existing application and the other is just a default MVC3 application...
I've already tried to look at differences in config, routing, etc. but nothing at the moment seems to give an indication where to look. The strangest thing is it only happens for nullable int's so far.
And what I get is an error in the ModelState dictionary for parameter saved
with the following exception:
System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' failed. See the inner
exception for more information. ---> System.Exception: True is not a valid value for Int32. ---> System.FormatException: Input string was not in a correct format.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.NullableConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
--- End of inner exception stack trace ---
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)
It looks like a bug to me. It basically tries to convert the value to int.. While the parameter type is Nullbable<bool>. What the heck?
Another funny thing is that I was only able to debug this error once. Now when I debug I don't get this error. Once I stop debugging the error comes back and I get a validation error.. This is funny. It's running on IIS not Cassini.
I also tried with Cassini - same luck. I get this error once in a while - usually when I change configurations and rebuild the solution. It's quite unpredictable. It somehow also affects Html.RenderPartial("PartialName", nullableParameter); The nullableParameter
is sometimes parsed correctly and sometimes not. This is very wierd..
I have encountered the same issue in my application. Given a controller action method with this signature:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SelectCompany(string name, int? connectionID)
It looks like the ModelBinder is confused when trying to bind connectionID from a POST. For some reason, when doing this, it thinks connectionID is ModelType = Nullable<bool>
Obviously, this is wrong, so the model binding fails. This was not happening in MVC3 RC1. Any instructions on how to roll-back to RC1?
Thank you for the reports. We've confirmed that this is a bug that was introduced with RC2. To work around this issue until a fix is available, please add the following line to your Global.asax file in the Application_Start method:
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
The bug exists in one of the caching systems in the MVC runtime, and the above line disables that particular cache.
Marked as answer by rekna on Dec 12, 2010 03:34 PM
rekna
Member
120 Points
58 Posts
ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 10, 2010 10:15 PM|LINK
Since ASP.NET MVC3 RC2 I encounter a bug when posting values to a controller method of which one of the parameter is a nullable int.
Steps to reproduce:
I've created a test method
[HttpPost]
public ActionResult PostTest(string from, int? number, bool? formsearch)
{
return new ContentResult { Content = string.Format("{0}/{1}/{2}", from, number, formsearch) };
}
using jquery, I create a Post request
$.post("http://localhost/mysite/test/posttest",{ from:"1//1/2009",number:"156",formsearch:true});
the request (eg. in fiddler) shows clearly the values are being sent:
from:1//1/2009
number:156
formsearch:true
but the result returned from this function is:
1//1/2009//True
If I change int? number to int number, the result is correct:
1//1/2009/156/True
In MVC3 RC1 this was working without any problems with the nullable int
Update: I don't seem to have the problem with a newly created MVC3 website. What could I have in my project that influence model binding to nullable int's? And why would there be a difference between RC1 and RC2? Anyone have suggestions for debugging this model binding problem?
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> [HttpPost]</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public ActionResult PostTest(string van, int soort, bool? FormSearch)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return new ContentResult { Content = string.Format("{0}/{1}/{2}", van, soort, FormSearch) };</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> } [HttpPost]</div><div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public ActionResult PostTest(string van, int soort, bool? FormSearch)</div><div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div><div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return new ContentResult { Content = string.Format("{0}/{1}/{2}", van, soort, FormSearch) };</div><div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }[</div>marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 04:45 PM|LINK
Could you do a diff of your two projects and see what's different. I can't think of anything that would affect this, but maybe something will jump out?
ASP.NET Team
@marcind
Blog
rekna
Member
120 Points
58 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 05:45 PM|LINK
Quite difficult to do, because one is a rather large existing application and the other is just a default MVC3 application...
I've already tried to look at differences in config, routing, etc. but nothing at the moment seems to give an indication where to look. The strangest thing is it only happens for nullable int's so far.
maksm
Member
6 Points
3 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 07:05 PM|LINK
I have exactly the same problem. I've got the following action method:
public ActionResult Edit(long id, bool? saved) { var model = new CustomerEditModel { Categories = this._service.GetCategories(), Customer = this._service.GetCustomer(id), Saved = saved.HasValue && saved.Value }; return this.View(model); }Request.Url:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">+ Url {http://tenant1.mpss.pl:8989/Dictionaries/Customer/Edit/1?saved=True} System.Uri</div> <div></div>Url {http://localhost:8989/Dictionaries/Customer/Edit/1?saved=True} System.Uri
And what I get is an error in the ModelState dictionary for parameter saved with the following exception:
System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' failed. See the inner exception for more information. ---> System.Exception: True is not a valid value for Int32. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.NullableConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
--- End of inner exception stack trace ---
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)
It looks like a bug to me. It basically tries to convert the value to int.. While the parameter type is Nullbable<bool>. What the heck?
Another funny thing is that I was only able to debug this error once. Now when I debug I don't get this error. Once I stop debugging the error comes back and I get a validation error.. This is funny. It's running on IIS not Cassini.
I also tried with Cassini - same luck. I get this error once in a while - usually when I change configurations and rebuild the solution. It's quite unpredictable. It somehow also affects Html.RenderPartial("PartialName", nullableParameter); The nullableParameter is sometimes parsed correctly and sometimes not. This is very wierd..
mvc3 rc2 DefaultModelBinder
anglicangeek
Member
24 Points
7 Posts
Microsoft
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 10:47 PM|LINK
rekna, is the default MVC 3 application you created also doing a jQuery post to the action?
linusconcepc...
Member
42 Points
9 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 11:02 PM|LINK
I have encountered the same issue in my application. Given a controller action method with this signature:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SelectCompany(string name, int? connectionID)
It looks like the ModelBinder is confused when trying to bind connectionID from a POST. For some reason, when doing this, it thinks connectionID is ModelType = Nullable<bool>
Obviously, this is wrong, so the model binding fails. This was not happening in MVC3 RC1. Any instructions on how to roll-back to RC1?
mvc3 rc2 DefaultModelBinder
levib
Star
7702 Points
1099 Posts
Microsoft
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 11, 2010 11:50 PM|LINK
Thank you for the reports. We've confirmed that this is a bug that was introduced with RC2. To work around this issue until a fix is available, please add the following line to your Global.asax file in the Application_Start method:
The bug exists in one of the caching systems in the MVC runtime, and the above line disables that particular cache.
maksm
Member
6 Points
3 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 12, 2010 06:16 AM|LINK
Thanks, this workaround works for now.
Still RC2 has some excellent features. Can't wait for RTM.
rekna
Member
120 Points
58 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 12, 2010 03:34 PM|LINK
Workaround indeed solves the problem... glad the bug was reproducible and the problem was found so quickly!
Thx all.
supmize
Member
2 Points
4 Posts
Re: ASP.NET MVC3 RC2 bug binding from request parameters to method parameters
Dec 13, 2010 01:36 AM|LINK
It solved my problem, too. Originally I thought it had something to do with the model binding to a list, but this work-around fixes things up.
I blogged about it here:
http://www.distribucon.com/blog/ASPNETMVC3RC2BindingErrors.aspx
Dan Miser