Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work around MVC4's breaking change in how it renders bools in a view

We recently upgraded an existing .NET 4 MVC 3 project to .NET 4.5 and MVC 4.

Where we had this sort of thing in a view:

@Model.MyBool
<input type="hidden" name="foo" value="@Model.MyBool" />

that used to render as:

True
<input type="hidden" name="foo" value="True" />

or:

False
<input type="hidden" name="foo" value="False" />

But now it renders as:

True
<input type="hidden" name="foo" value="value" />

or:

False
<input type="hidden" name="foo" />

That is, where a boolean property is rendered in the view as the value of a hidden input's value attribute, it doesn't render as True or False (as it does elsewhere), but rather renders as value, or misses the attribute altogether.

Two questions:

  1. WTF?
  2. Is there a nice easy way I can fix the multitude of places that this has broken my application? It's a big application and I don't fancy trawling through every single view to try to identify everywhere I put a bool into an input field.
like image 643
teedyay Avatar asked Aug 31 '12 19:08

teedyay


2 Answers

WTF?

Razor 2 conditional attributes

Is there a nice easy way I can fix the multitude of places that this has broken my application?

None I can think of (that's why you should have used @Html.Hidden("foo", Model.MyBool)). My best guess would be using something like Resharper's Structured Replace.

like image 59
Serg Rogovtsev Avatar answered Sep 28 '22 13:09

Serg Rogovtsev


looks like this is working:

<input type="hidden" name="foo" value="@Model.MyBool.ToString()" />
like image 21
0lukasz0 Avatar answered Sep 28 '22 13:09

0lukasz0