Copied to clipboard

Flag this post as spam?

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


  • Rihab 104 posts 388 karma points
    Feb 26, 2019 @ 14:00
    Rihab
    0

    creating Resource to get a database value

    Hi,

    I want to get a value from the database and so I followed this tutorial: Add-ServerSide-Data

    but the Data (Image url ) is not showing ?? and this is my code :

    1- ApiController:

     [Umbraco.Web.Mvc.PluginController("Sawah")]  
    public class AframeApiController : UmbracoAuthorizedJsonController
    {
        public mainimg GetMain()
        {
            DataRow[] Accts =DB_Oprs.GetVRMainImg(11359);
            mainimg collection = new mainimg();
            foreach(DataRow r in Accts)
            { 
            collection.versionId = r["versionId"].ToString();
            collection.mediaPath = r["mediaPath"].ToString();
            }
            return collection;
        }
    
        public IEnumerable<mainimg> GetPinsinfo()
        {
            DataRow[] Accts = DB_Oprs.GetVRMainImg(11359);
            IEnumerable<mainimg> collection = Accts.Cast<mainimg>();
            return collection;
        }
    }
    public class mainimg
    {
        public string versionId { set; get; }
        public string mediaPath { set; get; }
    }}
    

    2 -added this to umbraco.resources.js :

      angular.module('umbraco.resources').factory('AframeResource',
        function ($q, $http, umbRequestHelper) {
            // the factory object returned
            return {
                // this calls the ApiController we setup earlier
                getMain: function () {
                    return umbRequestHelper.resourcePromise(
                        $http.get("backoffice/Sawah/AframeApi/GetMain"),
                        "Failed to retrieve GetMain inmage");
                },
                getPins: function () {
                    return umbRequestHelper.resourcePromise(
                        $http.get("backoffice/Sawah/AframeApi/GetPinsinfo"),
                        "Failed to retrieve GetPins data");
                }
            };
        }
    );
    

    3- this is the angular controller code

    angular.module("umbraco")
    .controller("My.ContentController", function ($scope, AframeResource){
        AframeResource.getMain().then(function(response){
            $scope.MainImg = response.data;
        });
    });
    

    4- this is the html page :

    <div ng-controller="My.ContentController">
    <ul>
        <li>
            {{MainImg.mediaPath }}
        </li>
    </ul>
    

    5- this is the package.manifest

    "propertyEditors": [
    {
      "name": "Content values",
      "alias": "content.values",
      "group": "Content",
      "icon": "icon-map-location",
      "editor": {
        "view": "~/App_plugins/TestMyResource/Test.html",
        "valueType": "STRING"
      }
    }  ],  "javascript": [
    "~/App_plugins/TestMyResource/Content.controller.js"  ]
    

    can anyone help me ??

  • Lewis Logan 21 posts 132 karma points
    Feb 26, 2019 @ 16:57
    Lewis Logan
    0

    Hi,

    Are you able to provide a bit more on what you are trying to achieve?

    For example if you have the ID of the media stored in umbraco then you could grab the path using something like this:

    string mediaUrl = "";
    
                //get media using id
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var media = umbracoHelper.Media(id);
                mediaUrl = media.url;
    

    In regards to the code you've provided, are you seeing the versionID pull through as that's what is stated in your html?

    Also have you debugged the code and is mediaPath being returned ok?

    Kind Regards, Lewis

  • Rihab 104 posts 388 karma points
    Feb 27, 2019 @ 08:10
    Rihab
    0

    Hi Lewis,

    I want to create a custom property editor that shows data from the current content page...

    and yes I debugged the code and the controller is retrieving the imagepath. but in the HTML view, nothing is showing....

    thanks.

  • Lewis Logan 21 posts 132 karma points
    Feb 27, 2019 @ 10:36
    Lewis Logan
    0

    Hi again,

    Just trying to narrow down where the problem may lie. Are you able to debug and see what response.data returns? Does that have the expected values?

    Kind Regards, Lewis

Please Sign in or register to post replies

Write your reply to:

Draft