1

In my Form, there is a textbox, in razor code using EditorFor and the type given for this is Char. But it always shows a '�' as default, what's that mean? is this because of char type?

please look at the pictureenter image description here

Dhanil Dinesan
  • 575
  • 9
  • 27
  • its value is '�' – Dhanil Dinesan Apr 21 '20 at 09:21
  • so where does that value come from? do you load it from a database? I'd suggest to watch it in the debugger. Include relevant parts of your source code so someone here might be able to figure it out. Does the problem disappear if you temporarily change the data type to `System.String`? related: https://stackoverflow.com/q/15276191/1132334 – Cee McSharpface Apr 21 '20 at 09:23
  • @CeeMcSharpface If the field have value then it works fine, if it has no value ie the form open fresh, then it shows this symbol. Maybe it's because of the char type I don't know correctly. – Dhanil Dinesan Apr 21 '20 at 09:30
  • 1
    the default value of the `char` data type is '\0' (U+0000) - looks like that's substituted by the invalid unicode code point glyph you see. Assign a space (' ') instead. Or question the design decision to represent quantity types by a char, make it a string. Related: https://stackoverflow.com/q/43662939/1132334 – Cee McSharpface Apr 21 '20 at 09:32
  • @CeeMcSharpface Ok thank you, let me try with it – Dhanil Dinesan Apr 21 '20 at 09:37

1 Answers1

2

This � is the invalid unicode code point glyph. It is what you will see in most browsers when they encounter the null character '\0' (U+0000). This happens to be the default value of the .NET char data type.

Three workarounds:

  1. substitute a valid char for the default, for example a space: c == default(char) ? ' ' : c
  2. add logic to hide the EditorFor or LabelFor when the value is default(char)
  3. change the data type to System.String, which leads to correct rendition in both cases null and String.Empty
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77