Copied to clipboard

Flag this post as spam?

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


  • Gevorg 13 posts 124 karma points
    Sep 17, 2019 @ 10:43
    Gevorg
    0

    SurfaceController with custom database - IScopeProvider

    Dear community,

    I have upgraded my website from v7 to v8 and currently I am trying to port my package to v8. I do face some issues regarding access the database context. My case is that I load a list of countries from database. Previously I was calling the Surface controller from Macro partial view, by creating a country controller object and populating list from it.

          @{ 
            CountryController cc = new CountryController(); 
    }
         <select class="uk-select" name="fromcountry" autocomplete="off">           
                                      @foreach (Country c in cc.GetAll().Where(c => c.Visible != false))
                                      {
                                          <option value="@c.Id">@(iseng ? c.NameEN : c.NameAM)</option>
                                      }
    
    
                                  </select>
    

    Country controller looks likes this after modifying it to work with V8.

         public class CountryController : UmbracoAuthorizedApiController
            {
                private readonly IScopeProvider scopeProvider;
    
                public CountryController(IScopeProvider scopeProvider)
                {
                    this.scopeProvider = scopeProvider;
                }
                public List<Country> GetAll()
                {
                    using (var scope = scopeProvider.CreateScope(autoComplete: true))
                    {
                        var sql = scope.SqlContext.Sql()
                            .Select<Country>().From("cstCountry");
        var results scope.Database.Fetch<Country>(sql);
    scope.Complete()
                        return results;
    
    
                    }
                }
        }
    

    The issue I have is that I cannot call this controller from Macro partial, as the IScopeProvider works with dependency injection and I cannot pass it as parameter.

    I know that I might be making some serious and obvious mistake. Maybe I should convert my controller to ApiController and read values with Ajax requests? Please assist if you have any solution on your mode.

Please Sign in or register to post replies

Write your reply to:

Draft