Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Ligtenberg 53 posts 214 karma points
    May 23, 2015 @ 22:42
    John Ligtenberg
    1

    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);

    IEnumerable<IPublishedContent> cars = segmentRoot.Descendants(DocumentTypes.CarPage).ToList();

       // cars.As<CarPage>() returns null if the culture parameter is lacking.
       IEnumerable<CarPage> typedCarList = cars.As<CarPage>(culture:cultureInfo).ToList();

       return View("~/Views/Partials/CarList.cshtml", typedCarList);
        }
    }
  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    May 23, 2015 @ 22:46
    Lee Kelleher
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft