In my asp.net-mvc website I have a field that usually has a string (from database) but can from time to time contain nothing. Because IE doesn’t know how to handle the css ’empty-cells’ tag, empty table cells need to be filled with an
I thought
Html.Encode(' ');
would fix this for me, but apparantly, it just returns ‘ ‘. I could implement this logic as follows
Html.Encode(theString).Equals(' ')?' ':Html.Encode(theString);
Also a non-shorthand-if would be possible but frankly, both options are but ugly. Isn’t there a more readable, compact way of putting that optional space there?
A space encode in HTML is just a space. nbsp may look like a space, but has a different semantics, ‘non-breaking’ meaning that line breaks are suppressed.
Solution: Whenever I find functionality lacking or with unexpected behavior (e.g. asp:Label and HyperLink don’t HTML encode), I write my own utilities class which does as I say 😉