ASP.NET MVC3 Model Binding Error

I found a weird script that prevents bool? correctly sent back to the controller. This is a very specific problem, so follow the instructions to recreate.

The application must be deployed as a virtual folder in IIS, so that instead of / Home / Test URL / Virtual / Home / Test.

Home controller:

[HttpGet] public ActionResult Test(int? temp, bool? testBool) { return View(testBool); } 

/ Home / Testing (Razor cshtml):

 @model bool? @{ ViewBag.Title = "Test"; } @using (Html.BeginForm("Test", "Home", FormMethod.Get)) { @Html.CheckBox("testBool", Model ?? false, new { onchange = "this.form.submit();" }) @Html.Label(Model == true ? "True" : "False") } 

In response to a question about bool? doesn't get to the controller due to int? in front of it in the parameter list. Can this be solved by installing bool? before int? in the parameter list, but obviously you don't need to do this. It also works great if not in a virtual folder in IIS. The same problem exists when using the POST method, although you send bool instead of bool? It works, but is not necessary, if not in a virtual folder, so you do not need to do this either.

Has anyone else experienced this and is there anything that explains why the binding is failing or is it just a bug in MVC3?

If this is just an error, does anyone know what are the proper ASP.Net MVC channels for reporting error messages?

Update:

I found that if there are any number of variables with a zero value in the action parameters, only the first one will work, and all the rest will not be filled. Does anyone know if this is a design or bug?

+7
source share
2 answers

This is apparently still a problem. Thought it would be fixed by now. One way to fix this error is to add this to your Application_Start method.

 ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider(); 

Adding this parameter will allow you to behave correctly with zero parameters. I am surprised that this error still exists since it was known as RC2 in December.

The difference between optional route parameters in a disaggregated article is that routing is not a problem in your example. In this case, you publish (or request) the data and do not fill out the null action parameters properly. I checked the source of mvc3 and don’t understand why this should be a problem, since debugging it seems to work correctly, and going through the original result leads to the expected behavior. I'm pretty sure this has not been fixed, although Scott Guthrie said it should be by now ...

+1
source
+3
source

All Articles