Copied to clipboard

Flag this post as spam?

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


  • bh 405 posts 1382 karma points
    Mar 09, 2022 @ 15:50
    bh
    0

    Partial View URL for AJAX

    I'm attempting to use a partial view to retrieve data for another page via ajax.

    When I browse to /Views/Partials/MyPartial.cshtml I get a 404.

    What am I missing?

  • Carlos Mosqueda 240 posts 431 karma points
    Mar 09, 2022 @ 19:42
    Carlos Mosqueda
    100

    You can not browse directly to a partial view in the browser because partial views are just templates not actual served up pages.

    You can retrieve the rendered page but not the partial view itself, UNLESS you are going through a controller to populate the data via your model.

    So: 1. Are you populating your partial view with data via a controller then returning your partial view via the controller? 2. Are you just wanting to return a page with content on it that usese you partial view?

  • bh 405 posts 1382 karma points
    Mar 09, 2022 @ 20:32
    bh
    0

    Figured this was the issue, but wasn't sure how to overcome it. I'd rather not expose a page that can be browsed to and SPAMMED, but I don't know how else to get the data out of Umbraco database any other way.

    I'm using modelsbuilder dll mode with the API enabled. Is there a way to attach to an endpoint and get the response i'm after directly from the api?

    Here's my partial maybe that'll answer your question.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<myapplication.ModelsLibrary.Portfolio>
    
    @{ 
        Layout = null;
    
        var pageSize = 1;
    
        if (!string.IsNullOrEmpty(Request.QueryString["pageSize"]))
        {
            pageSize = Convert.ToInt32(Request.QueryString["pageSize"]);
        }
    
        var skip = 0;
    
        if (!string.IsNullOrEmpty(Request.QueryString["skip"]))
        {
            skip = Convert.ToInt32(Request.QueryString["skip"]);
        }
    
        var projects = Model.Children.Where(x => x.IsVisible()).Skip(skip).Take(pageSize);
    
    }
    
    
    <div id="portfolio-more">   
    
        @foreach(myapplication.ModelsLibrary.Project p in projects)
        {
            <a href="@p.Url()" class="row list-item @myapplication.ModelsLibrary.ClassName.GetStateByName(p.Address.Address.State.ToUpper())">
                <div class="col-12 col-lg-6">
                    <h3>@p.Name</h3>
                </div>              
                <div class="col-12 col-lg-6"> Other stuff here </div>
    
            </a>
        }
    
    </div>
    

    Here's my ajax call:

    if ($('#load-more').length) {
        $('#load-more').click(function () {
            var pageSize = $(this).data('page-size');
            var currentPage = $(this).data('current-page');
            var count = $(this).data('count');
            var moreUrl = $(this).data('url');
    
            if (parseInt(pageSize * currentPage) >= count) {
                $(this).hide();
            }
    
            $.ajax({
                url: moreUrl,//'/views/partials/PortfolioListMore.cshtml',
                Type: 'get',
                data: {
                    skip: parseInt(currentPage * pageSize),
                    pageSize: pageSize
                },
                success: function (html) {
                    $('#more-container').append($('#portfolio-more', $(html)));
                    $(this).attr('data-current-page', parseInt(currentPage + 1));
                }
            });
    
        });
    }
    

    Here's my load more button for reference:

    <div id="more-container"></div>
    
        <div id="load-more" data-page-size="@pageSize" data-current-page="1" data-count="@projects.Count()" data-url="/views/partials/PortfolioListMore.cshtml">LOAD MORE</div>
    
  • bh 405 posts 1382 karma points
    Mar 09, 2022 @ 21:34
    bh
    0

    I've gone all down the rabbit hole of the routing and the api documentation, and I honestly don't know where to start.

    Trying to create a controller, but the return Html.Partial piece is a lil off.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace myapplication.Controllers
    {
        [Route("projects/[action]")]
        public class ProjectsListController : Umbraco.Web.WebApi.UmbracoApiController
        {
            [Route("projects/{page?}")]
            public string GetProjects(int skip, int pageSize)
            {
                return Html.Partial("/views/partials/PortfolioListMore.cshtml", new ViewDataDictionary { { "skip", skip },  { "pageSize", pageSize } })
            }
        }
    }
    
  • bh 405 posts 1382 karma points
    Mar 09, 2022 @ 22:20
    bh
    0

    I'm getting closer...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    
        namespace maypplication.Controllers
        {
    [Route("projects/[action]")]
        public class ProjectsListController : SurfaceController 
        {
            [Route("projects/{page?}/{pageSize?}")]
            public PartialViewResult GetProjects(int page, int pageSize)
            {
                return PartialView("/views/partials/PortfolioListMore.cshtml", new Umbraco.Web.Models.ContentModel(CurrentPage), new ViewDataDictionary { { "page", page }, { "pageSize", pageSize } });
            }
        }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft