1

It seems to be a fundamental question, but I could not find the exact answer.

I am passing a null or empty char value ('\0' in c#) from Model to View, and in View I need to check whether the char is equal to null or empty.

I have tried the following:

if ("@Model.CharValue" != "\0") {...}
if ("@Model.CharValue" != '\0') {...}
if ("@Model.CharValue" != ' ') {...}

None of them gave me the proper check. and using

alert("@Model.CharValue");

The pop up gave me this character, which I have no idea what it is.

enter image description here

How can I check whether a char is null or empty in Javascript when it is passed from Model to razor View?

oopsdazie
  • 716
  • 1
  • 7
  • 22
  • That looks to me as a wrongly encoded value, an UTF8 string being read as plain ASCII/ANSI – Gusman Apr 27 '17 at 16:14
  • 1
    Unicode replacement character? more or less browser saying "WTF is this?" What is the value? Assumedly stored in a db. – Tez Wingfield Apr 27 '17 at 16:35
  • The value is a null or empty char, in c# it is '\0'. And Thank you, I have edited my question. – oopsdazie Apr 27 '17 at 17:11
  • 1
    In your first check I think it should be single quote `'\0'` instead of double quote `"\0"` – yeah-buddy Apr 27 '17 at 17:41
  • `if(c3 != 0 && !char.IsWhiteSpace(c3)){...}` works in c#, however I believe your real question is about javascript since you are saying that is what shows up in the alert box. – nurdyguy Apr 27 '17 at 17:46
  • @ nurdyguy Thanks, you just reminded me that I can actually use c# in razor view, instead of javascript...lol, yeah, you are right, it is about Javascript. – oopsdazie Apr 27 '17 at 18:08
  • Does this answer your question? [Razor code EditorFor showing '�' as default?](https://stackoverflow.com/questions/61339819/razor-code-editorfor-showing-as-default) – Cee McSharpface Apr 21 '20 at 09:59

0 Answers0