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.
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.
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?
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.
Partial view being called has the inherit:
Then does some rendering of map based upon passed in lat/long
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.
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?
Hi Simon,
I just looked in to the source of Umbraco...this should only happen when you pass in a empty content item
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
And if you pass it in as a string "1055"
I've seen something strange where integer did not work.
Dave
Ahem yup that appears to be it. Why on earth would that not work in this instance.
Many thanks Dave!
Don't know...had it on one website myself...never actually bothered to investigate :-)
is working on a reply...