Copied to clipboard

Flag this post as spam?

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


  • Christina 127 posts 390 karma points notactivated
    Jun 06, 2018 @ 08:16
    Christina
    0

    How can I use Modelsbuilder in a surfacecontroller

    Hi Im implementing Seo and following Paul Seal example https://codeshare.co.uk/blog/how-to-optimise-your-website-for-search-engines-seo-in-umbraco/

    In the surfacecontroller I want to use the modelsbuilder instead of Currentpage

     namespace FionaWhitfieldArt.Controllers
    {
        public class SiteLayoutController : SurfaceController
        {
            private string PartialViewPath(string name)
            {
                return $"~/Views/Partials/SiteLayout/{name}.cshtml";
            }
    
            public ActionResult RenderMetaData()
            {
                MetaDataModel model = new MetaDataModel();
                IPublishedContent homePage = CurrentPage.AncestorOrSelf("home");
                string domainAddress = homePage.UrlWithDomain();
                string title = CurrentPage.GetPropertyValue<string>("title");
                model.Title = !string.IsNullOrEmpty(title) ? title : CurrentPage.Name;
                model.Description = CurrentPage.HasProperty("description") ? CurrentPage.GetPropertyValue<string>("description") : null;
                model.Keywords = CurrentPage.HasProperty("keywords") ? CurrentPage.GetPropertyValue<string>("keywords") : null;
                if(CurrentPage.HasProperty("socialShareImage"))
                {
                    int mediaId = CurrentPage.GetPropertyValue<int>("socialShareImage");
                    var mediaItem = Umbraco.Media(mediaId);
                    model.ImageUrl = $"{domainAddress.TrimEnd('/')}{mediaItem.Url}";
                }         
                model.Url = CurrentPage.UrlWithDomain();
                return PartialView(PartialViewPath("_MetaData"), model);
            }
    
        }
    }
    

    Many Thanks/Christina

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 06, 2018 @ 08:50
    Ismail Mayat
    100

    Try,

    var strongCurrentPage = CurrentPage as Whatever your type is ?

    Regards

    Ismail

  • Christina 127 posts 390 karma points notactivated
    Jun 06, 2018 @ 15:09
    Christina
    0

    Hi Ismail

    many thanks /Christina

Please Sign in or register to post replies

Write your reply to:

Draft