如何避免“空”的字符串在客户端绑定JSON数据时 [英] How to avoid 'null' strings when binding JSON data on client side

查看:112
本文介绍了如何避免“空”的字符串在客户端绑定JSON数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能避免'NULL'刺在客户端绑定JSON数据时HTML UI?

Is it possible to avoid having 'NULL' stings on the client when binding JSON data to HTML UI?

我使用ASP.NET MVC + jQuery的+ jTemplates 。数据从LINQ到SQL类到来,这些类有相当多的可空属性。当这样的性能得到序列化并传输回客户端我结束了这样的JSON:

I'm using ASP.NET MVC + jQuery + jTemplates. Data is coming from linq-to-sql classes and these classes have quite a lot of nullable properties. When such properties get serialized and transferred back to client I end up with such JSON:

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

乳清我绑定此数据为HTML我有很多'NULL'的字符串。我都试过手工绑定和JavaScript的模板引擎(jTemplate)。结果是相同的。
目前,我被'合并'的空值处理这个问题如下:

Whey I bind this data to HTML I have a lot of 'NULL' strings. I've tried both manual binding and JavaScript templating engines (jTemplate). Results are the same. Currently I'm dealing with this issue by 'coalescing' the null values as follows:

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

但我不想做手工。

But I don't want to do it manually.

请指教,如果我:


  1. 可以自动调整两种序列化过程或者选择第三方JSON序列化过的.NET JSON序列转化为空的属性为空字符串。

  2. 可以对客户端的东西,比如解决此工作与jQuery的两种或模板引擎。

感谢您。

推荐答案

您可以设置自定义序列化(见

You could set up custom serialization (see

<一个href=\"http://stackoverflow.com/questions/159704/how-to-implement-custom-json-serialization-from-asp-net-web-service\">http://stackoverflow.com/questions/159704/how-to-implement-custom-json-serialization-from-asp-net-web-service )

您也可以使自己的val的版本,转换为空为空字符串。不过,我认为,目前正在使用的方法可能是更好无论如何 - 通用的方法可以补充大量的复杂性,可能是​​隐藏的bug

You could also make your own version of val that converts null to an empty string. However, I think that the method you are currently using is probably better anyway - the generic methods could add a lot of complexity and possibly hidden bugs.

这篇关于如何避免“空”的字符串在客户端绑定JSON数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆