teedyay
teedyay

Reputation: 23531

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.

Upvotes: 6

Views: 872

Answers (2)

Sergei Rogovtcev
Sergei Rogovtcev

Reputation: 5832

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.

Upvotes: 3

0lukasz0
0lukasz0

Reputation: 3277

looks like this is working:

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

Upvotes: 2

Related Questions