Copied to clipboard

Flag this post as spam?

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


  • Marko Jovanov 10 posts 115 karma points
    Nov 20, 2015 @ 11:12
    Marko Jovanov
    1

    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 _Sales partial inherits the following:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<SaleViewModel>>

    The click button jQuery event:

    $('.js-reload-details').on('click', function (evt) {
        $('div#loading').html('<img src="/images/ajax-loader.gif" />');
        evt.preventDefault();
        evt.stopPropagation();
    
        var $detailsDiv = $('#detailsDiv'),
            url = $(this).data('url');
    
        $.get(url, function (data) {
            $detailsDiv.replaceWith(data);
        });
    
        $('div#loading').empty();
    });
    

    And the button click is implemented like this:

    <a id="loadMore" class="js-reload-details cta" data-url="@Url.Action("LoadMoreSales", "Calendar", new { months = -2})" href="javascript:;">@Umbraco.GetDictionaryValue("Calendar.LoadMore")</a>
    

    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

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 20, 2015 @ 11:23
    Ismail Mayat
    1

    Marko,

    This was in v6 but similar issue, so in the partial i did

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    

    Regards

    Ismail

  • Marko Jovanov 10 posts 115 karma points
    Nov 20, 2015 @ 11:29
    Marko Jovanov
    0

    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?

  • Marko Jovanov 10 posts 115 karma points
    Nov 23, 2015 @ 12:36
    Marko Jovanov
    103

    EDIT: Fixed implementation added

    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:

    <div id="cultureInfo" hidden>@System.Threading.Thread.CurrentThread.CurrentCulture</div>
    

    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.

  • Jan Bengtsson 64 posts 108 karma points
    Jan 21, 2016 @ 15:20
    Jan Bengtsson
    0

    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:

    • 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!

    /Jan

  • Jan Bengtsson 64 posts 108 karma points
    Jan 22, 2016 @ 14:40
    Jan Bengtsson
    0

    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

  • Marko Jovanov 10 posts 115 karma points
    Apr 11, 2016 @ 07:03
    Marko Jovanov
    0

    Hi Jan,

    I am glad that you resolved the issue!

    Regards, Marko

Please Sign in or register to post replies

Write your reply to:

Draft