I am having a problem returning a JsonNetResult for an object when it is a property of another object, however when I fetch the object explicitly it works e.g.

JsonNetResult res = new JsonNetResult();  
 res.Formatting = Formatting.Indented;  
 res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;  
 res.Data = addressRepository.Get(7);  
 return res;

returns a valid result however

JsonNetResult res = new JsonNetResult();  
res.Formatting = Formatting.Indented;  
res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;  
res.Data = businessRepository.Get(businessID).Address;  
return res;

will return an empty object; as will

JsonNetResult res = new JsonNetResult();  
res.Formatting = Formatting.Indented;  
res.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
res.Data =  addressRepository.Get(businessRepository.Get(businessID).Address.Id);  
return res;`

even though the address and it's ID is exactly the same in each situation. Is there something really obvious I'm missing?

有帮助吗?

解决方案

This sounds like a lazy-load problem, which happens when trying to hydrate Json objects. Is the address property a string or does it reference another object? Make sure there's no recursive references, though JsonNetResult should deal with that.

Go into your entity's mapping and add this:

mapping.References(x => x.Address).Not.LazyLoad();

That should take out any lazy load proxies out of the equation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top