Umbraco AJAX partial view controller action call - unable to retrieve the Umbraco.Context
I have the following scenario: Calendar page that loads the initial sale nodes from last 2 months. This page has a Load more button that fetches more Sale items that are 2 additional months of Sales.
I have added the call to the to controller action like this:
public ActionResult LoadMoreSales(int months = 0)
{
if (Request.IsAjaxRequest())
{
if (Request.QueryString["department"] == null)
{
return PartialView("Calendar/_Sales", GetSales(0, months));
}
else
{
int depId = 0;
Int32.TryParse(Request.QueryString["department"], out depId);
return PartialView("Calendar/_Sales", GetSales(depId, months));
}
}
else
{
return PartialView("Calendar/_Sales");
}
}
GetSale retrieves the list of SaleViewModel and the _Sales partial is used when accessing the Calendar page and when clicking Load more button. Through this method, the AJAX call is being made and the list is being retrieved.
The Sale items are being loaded, but I am unable to access UmbracoHelper and when I add macros to the page, the call breaks since there is no UmbracoContext reference from *UmbracoHelper. Also the following dictionary calls does not work:
I was able to resolve the issue by setting the CultureInfo before returning the PartialView like so: System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture)
The action method looks like this:
public ActionResult LoadMoreSales(int months = 0, string culture = "")
{
// Set the 'CultureInfo' to perserve 'UmbracoContext' when performing an AJAX call
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
if (Request.IsAjaxRequest())
{
if (Request.QueryString["department"] == null)
{
return PartialView("Calendar/_Sales", GetSales(0, months));
}
else
{
int depId = 0;
Int32.TryParse(Request.QueryString["department"], out depId);
return PartialView("Calendar/_Sales", GetSales(depId, months));
}
}
else
{
return PartialView("Calendar/_Sales");
}
}
I have added the hidden cultureInfo tag in the .cshtml:
I'm using a similar concept for ajax calls to surfacecontroller methods (that returns a partial view after retrieving values from Umbraco's dictionary).
And it works nicely. BUT only when running my project locally - NOT on a Windows Web Server! What might be the reason for that?
A few facts:
Umbraco version: 7.3.5
Database: same
Code: identical
Web server: IIS in both cases
.NET Framework: same version
Any thoughts that could point me in the right direction are most welcome!
Umbraco AJAX partial view controller action call - unable to retrieve the Umbraco.Context
I have the following scenario: Calendar page that loads the initial sale nodes from last 2 months. This page has a Load more button that fetches more Sale items that are 2 additional months of Sales.
I have added the call to the to controller action like this:
GetSale retrieves the list of SaleViewModel and the _Sales partial is used when accessing the Calendar page and when clicking Load more button. Through this method, the AJAX call is being made and the list is being retrieved.
The _Sales partial inherits the following:
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<SaleViewModel>>
The click button jQuery event:
And the button click is implemented like this:
The Sale items are being loaded, but I am unable to access UmbracoHelper and when I add macros to the page, the call breaks since there is no UmbracoContext reference from *UmbracoHelper. Also the following dictionary calls does not work:
@umbracoHelper.GetDictionaryValue("Calendar.ViewDetails")
Anybody had the similiar issue?
Related stack overflow post
Marko,
This was in v6 but similar issue, so in the partial i did
Regards
Ismail
Thank you for the answer Ismail.
I have just tried that and the umbracoHelper properties either throw an exception or they are null.
Is there something else I have to modify to ensure the correct context is loaded?
EDIT: Fixed implementation added
I was able to resolve the issue by setting the
CultureInfo
before returning thePartialView
like so:System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture)
The action method looks like this:
I have added the hidden
cultureInfo
tag in the.cshtml
:The jQuery was also modified to read and pass the hidden value to the controllers action like so:
There was no need to change
.cshtml
file, the dictionary values are being loaded correctly.Hi,
I'm using a similar concept for ajax calls to surfacecontroller methods (that returns a partial view after retrieving values from Umbraco's dictionary).
And it works nicely. BUT only when running my project locally - NOT on a Windows Web Server! What might be the reason for that?
A few facts:
Any thoughts that could point me in the right direction are most welcome!
/Jan
Resolved - by adding a second parameter when initializing the CultureInfo object:
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture, false)
According to MSDN, this boolean "...specifies whether to use the user-selected culture settings from the system."
Apparently there was an unwanted culture setting somewhere on the server.
/Jan
Hi Jan,
I am glad that you resolved the issue!
Regards, Marko
is working on a reply...