Umbraco AJAX partial view controller action call - unable to retrieve the Umbraco.Context

后端 未结 2 1151
我在风中等你
我在风中等你 2021-01-25 01:14

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<

2条回答
  •  独厮守ぢ
    2021-01-25 01:47

    EDIT: Added fixed implementation.

    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:

    
    

    The jQuery was also modified to read and pass the hidden value to the controllers action like so:

            var cultureInfo = $('#cultureInfo').html();
    
            $.get("/umbraco/surface/Calendar/LoadMoreSales?months=" + months + "&culture=" + cultureInfo, function (data) {
                if (data != '') {
                    $("#saleList").append(data);
                }
                else {
                    months = -1;
                    $("#saleList").append('No more news to display');
                }
    
                _inCallback = false;
                $('div#loading').empty();
            });
    

    There was no need to change .cshtml file, the dictionary values are being loaded correctly.

提交回复
热议问题