Copied to clipboard

Flag this post as spam?

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


  • Simon steed 345 posts 589 karma points
    Mar 13, 2018 @ 09:48
    Simon steed
    0

    Calling a surface controller from an AJAX call and custom RenderModel - giving null reference!

    Long post so forgive me :)

    Basically i'm working upon a 7.7.8 new build where I want to grab the users lat/long and pass through to a partial view to render results when they visit the page based upon their location. Straight forward enough. However to do this I need to grab the lat/long before the results are loaded, pass through to a controller, initialise a partial view and return that result back to the browser.

    So what i've got is the following code/logic.

    // Ajax call:
    var postData = { Lat: 53.481091, Long: -2.2373998, NodeId: 1055 };
            $.ajax({
                url: '/umbraco/surface/GridSurface/GetFilteredGrid',
                type: "POST",
                cache: false,
                data: JSON.stringify(postData),
                dataType: "json",
                contentType: 'application/json; charset=utf-8',
                error: function (data) {
                },
                success: function (data) {
                    // do something with data
                }
            });
    
    
    // surface controller:
    using System.Globalization;
    using System.Web.Mvc;
    using Models;
    using Umbraco.Web.Mvc;
    namespace MyControllers
    {
        public class GridSurfaceController : SurfaceController
        {
            [HttpPost]
            public PartialViewResult GetFilteredGrid(Location data)
            {
                //create a RenderModel based on some content ID
                var model = new PosViewModel(Umbraco.TypedContent(data.NodeId), CultureInfo.CurrentUICulture)
                {
                    Lat = data.Lat,
                    Long = data.Long
                };
    
                return PartialView("~/views/partials/GridLoader.cshtml", model);
            }
        }
    }
    

    Partial view being called has the inherit:

    @inherits UmbracoViewPage<Models.PosViewModel>
    

    Then does some rendering of map based upon passed in lat/long

    // Model:
    public class PosViewModel : RenderModel
        {
            public PosViewModel(IPublishedContent content) : base (content) { }
            public PosViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
            public PosViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
            public double Lat { get; set; }
            public double Long { get; set; }
        }
    

    Error is thrown initialising this PosViewModel in line base (content, culture) - says content is null however culture coming through fine!! Why would this be when i'm using Umbraco.TypedContent to pass a copy of that object in based upon the current page id.

    Error initialising render model

    Now I now I should normally use CurrentPage to init my model, however as posting from Ajax I don't have a page. This is why I pass in the nodeId and use Umbraco.TypedContent to sort this out for me, object is clearly valid before passing into the constructor, why does the model not allow it and state it's null? Umbraco bug? ModelsBuilder is set to LiveDll if that has any bearing on things.

    Any ideas how to sort this annoying problem out?

  • Dave Woestenborghs 2752 posts 8343 karma points MVP 3x admin c-trib
    Mar 13, 2018 @ 11:53
    Dave Woestenborghs
    0

    Hi Simon,

    I just looked in to the source of Umbraco...this should only happen when you pass in a empty content item

     public RenderModel(IPublishedContent content, CultureInfo culture)
    {
      if (content == null)
        throw new ArgumentNullException("content");
      if (culture == null)
        throw new ArgumentNullException("culture");
      this.Content = content;
      this.CurrentCulture = culture;
    }
    
  • Simon steed 345 posts 589 karma points
    Mar 13, 2018 @ 11:55
    Simon steed
    0

    Thanks Dave

    Never thought to check there doh, thanks. Strange thing is i'm passing in the nodeid of the homepage which is 1055 then using Umbraco.TypedContent(1055) to pass into the constructor - how can that be null when i've checked it's valid in debug?

    Any ideas?

    Simon

  • Dave Woestenborghs 2752 posts 8343 karma points MVP 3x admin c-trib
    Mar 13, 2018 @ 11:57
    Dave Woestenborghs
    100

    And if you pass it in as a string "1055"

    I've seen something strange where integer did not work.

    Dave

  • Simon steed 345 posts 589 karma points
    Mar 13, 2018 @ 12:04
    Simon steed
    0

    Ahem yup that appears to be it. Why on earth would that not work in this instance.

    Many thanks Dave!

  • Dave Woestenborghs 2752 posts 8343 karma points MVP 3x admin c-trib
    Mar 13, 2018 @ 12:10
    Dave Woestenborghs
    0

    Don't know...had it on one website myself...never actually bothered to investigate :-)

Please Sign in or register to post replies

Write your reply to:

Draft