Ditto .As returning null in controller called through an Ajax call.
Hello,
Recently I used Ditto in a SurfaceController called through an Ajax call, and I was surprised to see the conversion of my IEnumberable<IPublishedContent> return null when converting with the Ditto .As<> extension method.
It took me quite a while to figure out that the extension method required a value for the culture parameter. When hijacking a route with a controller inheriting from RenderMvcController this was not necessary, but apparently this culture context is missing when calling a controller through an Ajax call. I'm showing the code because I thought this was not obvious at all, it may save somebody some time.
John
public class CarController : SurfaceController { public ActionResult Get() { CultureInfo cultureInfo = new CultureInfo("nl-NL");
var segmentId = Convert.ToInt32(Request["segmentid"]); var segmentRoot = Umbraco.TypedContent(segmentId);
// cars.As<CarPage>() returns null if the culture parameter is lacking. IEnumerable<CarPage> typedCarList = cars.As<CarPage>(culture:cultureInfo).ToList();
Sorry for any headaches this has caused. Glad that you sorted it. Someone else raised a similar issue the other day, where the UmbracoContext was null - again related to the culture not being set.
The next version of Ditto doesn't have this issue, we have enhanced the culture detection. (We should be releasing v0.7.0 next week)
Thanks again for posting your fix/solution. It's very much appreciated.
Ditto .As returning null in controller called through an Ajax call.
Hello,
Recently I used Ditto in a SurfaceController called through an Ajax call, and I was surprised to see the conversion of my IEnumberable<IPublishedContent> return null when converting with the Ditto .As<> extension method.
It took me quite a while to figure out that the extension method required a value for the culture parameter. When hijacking a route with a controller inheriting from RenderMvcController this was not necessary, but apparently this culture context is missing when calling a controller through an Ajax call. I'm showing the code because I thought this was not obvious at all, it may save somebody some time.
John
Hi John,
Sorry for any headaches this has caused. Glad that you sorted it. Someone else raised a similar issue the other day, where the
UmbracoContext
was null - again related to the culture not being set.The next version of Ditto doesn't have this issue, we have enhanced the culture detection. (We should be releasing v0.7.0 next week)
Thanks again for posting your fix/solution. It's very much appreciated.
Cheers,
- Lee
is working on a reply...