Avoiding Null Strings When Binding Client-Side JSON Data

Is it possible to avoid "NULL" bites on the client when binding JSON data to HTML UI?

I am using ASP.NET MVC + jQuery + jTemplates . The data comes from linq-to-sql classes, and these classes have quite a few null properties. When such properties become serialized and passed back to the client, I get this JSON:

[{"Id":1,"SuitId":1,"TypeId":null,"Type":null,"CourtId":null,"Court":null}]

Whey I bind this data to HTML. I have many "NULL" lines. I tried both manual binding mechanisms and JavaScript templates (jTemplate). The results are the same. I am currently dealing with this problem by β€œcombining” null values ​​as follows:

$('#Elem').val(someVar||'');

But I do not want to do this manually.

I ask for advice if I:

  • It can automatically convert properties with a null value to empty strings by setting up the serialization process or, possibly, selecting a third-party JSON serializer over the .NET JSON serializer.
  • It can do something on the client side, for example, get around it using jQuery or templating mechanisms.

Thanks.

+5
source share
4 answers

You can set up custom serialization (see

How to implement custom JSON serialization from ASP.NET web service? )

val, null . , , , , , - , , .

+2

jtemplate, , jquery.jtemplate.js.

TemplateUtils.cloneData = function(d, filter, f_escapeString) {

- if

if (d == null) {
    return d;
}

if (d == null) {
   return "";
}

+3

, ? , TypeId null (, ), "'( )?

, , "", - . , . , "" .

, , , , , , . , ( ), , .

+1

jTemplates , :

{#if $T.mylist.nullableProperty != null}{$T.myList.nullableProperty}{#/if}
0

All Articles