Sunday, March 11, 2012

Nested Properties

hello.

can you show us a page that reproduces this?


Hi Luis,

Unfortunately, I am developing this on my home PC so am unable to make this publicly visible. I assume that you are actually able to traverse an IList property in this fashion and reference a Count of the items...is that right? My binding is as follows:

<bindings><binding dataPath="Customer.Orders.Count" property="text" /></bindings>

The C# property on the Customer class is as follows:

[DataObjectField(false)][XmlElement("Order")]public virtual IList Orders{get{return this._orders;}set{this._orders =value;}}
Is there something specific that I should be looking for to troubleshoot this?
Thanks,
Jason

hello.

i think your binding isn't quite right. if each row is a customer, the you should not need to indicate the type Customer. one more thing: if i'm not mistaken, those types are serialized as arrays and you should use the length property (use fiddler to see the json that is being returned from the web service.

btw, here's a quick page i've built in the past that shows that it's possible to bind to subproperties of an array (in this case, i've used the initialdata property of the data source control, but i think that everything should work when you get the data through a web service):

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

head><title>Untitled Page</title>

</

head>

<

body><scripttype="text/javascript"src="Atlas.js"></script><divstyle="display: none"><tableid="masterTemplate"><tbodyid="topElement"><trclass="header"><th>Nome</th><th>Idade</th><th>Count</th></tr><trid="itemTemplate"><tdid="idTemplate"></td><tdid="nomeTemplate"></td><tdid="count"></td></tr></tbody></table></div><divid="conteudo"></div><scripttype="text/xml-script">

<page xmlns:script=

"http://schemas.microsoft.com/xml-script/2005">

<components>

<dataSource id=

"mySource" autoLoad="true">

<initialData>

[{id:1, nome:

"Luis",classes:[{nomDisc:"portuguese"}, {nomeDisc:"english"}, {nomeDisc:"math"}]},{id:2, nome:"Rita", classes:[{nomDisc:"portuguese"},{nomeDisc:"english"}]}]

</initialData>

</dataSource>

<listView id=

"conteudo" itemTemplateParentElementId="topElement" itemCssClass="item"

alternatingItemCssClass=

"alternatingItem" selectedItemCssClass="selectedItem">

<bindings>

<binding dataContext=

"mySource" dataPath="data" property="data" />

</bindings>

<layoutTemplate>

<template layoutElement=

"masterTemplate" />

</layoutTemplate>

<itemTemplate>

<template layoutElement=

"itemTemplate">

<label id=

"idTemplate">

<bindings>

<binding dataPath=

"id" property="text" />

</bindings>

</label>

<label id=

"nomeTemplate">

<bindings>

<binding dataPath=

"nome" property="text" />

</bindings>

</label>

<label id=

"count">

<bindings>

<binding dataPath=

"classes.length" property="text" />

</bindings>

</label>

</template>

</itemTemplate>

</listView>

</components>

</page>

</script><scripttype="text/javascript">function h()

{

var lst = $object("conteudo");var content = lst.get_associatedElement();

content.removeNode(

true );

document.getElementById(

"o").appendChild( content );

}

</script>

</

body>

</

html>

Hey Luis,

Sorry, that was my mistake when entering the last post. The actual binding I had was "Products.Count".

Your observation of the incorrect use of Count instead of length has fixed the problem. I hadn't made the mental leap from the server side business objects to the client side JSON objects. All has become clear now!

I am now left with one problem though...circular references. My business objects have lots of instances where circular references area causing a problem with the serialization...do you have any recommendations about how to workaround this? I posted this problem to another forum yesterday:

http://forums.asp.net/thread/1266367.aspx

Thanks for your help.

Jason


hello.

though i haven't tried it, i think that applying the xmlignoreattribute will also stop the JSON serialization of a property exposed by a class.


Hi,

I just checked in with that other post that I made and someone had responded with a similar suggestion. I added the XmlIgnore attribute on the back reference and the object does indeed serialize OK. This will present a bit of a problem on deserialization though and in some cases it would be advantageous to be able to traverse from the child to the parent although it doesn't sound like you can have both :-(

Thanks,

Jason


hello again.

though i haven't used it yet, I think that you can provide your own javascript serializer. take a look at the converterssection by using .net reflector...

No comments:

Post a Comment