Copied to clipboard

Flag this post as spam?

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


  • Graham Davis 110 posts 376 karma points
    Dec 29, 2017 @ 16:43
    Graham Davis
    0

    Getting View to Load in backend

    Hi,

    I am trying load a View(MVC not angular) into a custom section and could use some help. It works if I use "return Content()", but nothing happens with I use "return View()". The form is empty.

    The forms in the view will be interacting with an external SQL Server database via an api. There is no need for a Tree with this section, so one is not being implemented. What am I doing wrong?

    I am using this as my guide: http://www.jondjones.com/learn-umbraco-cms/umbraco-developers-guide/customising-umbraco-ui/how-to-display-an-mvc-view-in-the-umbraco-backend

    Dashboard.config

    Controller

    public class SystemController : UmbracoAuthorizedController
    
    {
    public ActionResult AgentProfile()
        {
            try
            {
                //return Content("Hello");
                return View("/Views/AgentProfile.cshtml", new Agent());
            }
            catch (Exception ex)
            {
    
                throw;
            }        
    
        }
    

    }

    AgentProfile.cshtml

     @model Agent()
    

    Hello, It Works! @Model.FirstName @Model.LastName

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 02, 2018 @ 12:38
    Alex Skrypnyk
    0

    Hi Graham

    It's not working with View(), because View() is trying to return view, and you need plain html to return

    Thanks,

    Alex

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 02, 2018 @ 12:39
    Alex Skrypnyk
    0

    You can render view in the controller and return a plain html with this method:

    public string RenderViewToString(string viewName, object model)
    {
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
           var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
           var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
           viewResult.View.Render(viewContext, sw);
           viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
           return sw.GetStringBuilder().ToString();
         }
    }
    
  • Graham Davis 110 posts 376 karma points
    Jan 04, 2018 @ 23:39
    Graham Davis
    100

    I was able to get it working. Not sure if there is a better way or not, but I found a Umbraco JavaScript function [UmbClientMgr.content (url)] that provided a way to get it to work. The only issue I am having now is that the function causes the "spinner" to show and I have not figured out how to get rid of it. If you know how to remove it, please let me know.

    THE SHORT VERSION of getting it to work is to use the page in your Dashboard.config file as a relay to load your MVC page.

    In your Dashboard.config:
    

    Then in LaodDefault.html,

    <script>
        UmbClientMgr.contentFrame("/umbraco/backoffice/Plugins/AgentAdmin/AgentProfile");
    </script>
    

    THE LONG VERSION is posted below. I would have preferred to post this in a zip, but that is not an available option.

    File Structure

    RouteConfig.cs

    using System.Web.Mvc; using System.Web.Routing;

    namespace YourProject.AppPlugins.AgentAdmin.AppStart { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); RouteTable.Routes.MapRoute( "AgentAdmin", "umbraco/backoffice/Plugins/AgentAdmin/{action}/{id}", new { controller = "AgentAdmin", action = "AgentProfile", id = UrlParameter.Optional }); } } }

    WebApiConfig.cs

    using System.Web.Http;

    namespace YourProject.AppPlugins.AgentAdmin.AppStart { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }

    AgentProfileController.cs

    using System; using System.Web.Mvc; using Umbraco.Web.Mvc;

    namespace YourProject.App_Plugins.AgentAdmin.Controllers { public class AgentAdminController : UmbracoAuthorizedController { public ActionResult AgentProfile() { try { Agent agent = new Agent(); agent.FirstName = "Mickey"; agent.LastName = "Mouse";

                return View("~/App_Plugins/AgentAdmin/Views/AgentProfile.cshtml", agent);
            }
            catch (Exception ex)
            {
                throw;
            }
    
        }
    
    
    }
    

    }

    AgentProfile.cshtml

    @model Agent

    Hello @Model.FirstName @Model.LastName

    LoadDefault.html

    <script>
    UmbClientMgr.contentFrame("/umbraco/backoffice/Plugins/AgentAdmin/AgentProfile");
    

    Web.config

    AgentAdminSection.cs

    using umbraco.businesslogic; using umbraco.interfaces;

    namespace YourProject.AppPlugins.AgentAdmin .Umbraco.EditorTools.AppPlugins.EditorTools { [Application("agentProfiles", "Agent Profiles", "icon-name-badge", 10)] public class AgentProfiles : IApplication { } }

    AgentAdminTree.cs

    using System; using System.Collections.Generic; using umbraco.businesslogic; using umbraco.BusinessLogic.Actions; using umbraco.cms.presentation.Trees; using umbraco.interfaces;

    //Stubbing if needed later namespace YourProject.App_Plugins.AgentAdmin { //[Tree("AgentAdmin", "Agent Admin", "TBD")] //public class EditorToolsTree : BaseTree //{ // public EditorToolsTree(string application) // : base(application) // { // }

        //    protected override void CreateRootNode(ref XmlTreeNode rootNode)
        //    {
        //        rootNode.NodeType = "example";
        //        rootNode.NodeID = "init";
        //        rootNode.Menu = new List<IAction> { ActionRefresh.Instance };
        //    }
    
        //    public override void Render(ref XmlTree tree)
        //    {
        //        var IndexNode = XmlTreeNode.Create(this);
        //        IndexNode.NodeID = "0";
        //        IndexNode.NodeType = "Home";
        //        IndexNode.Text = "Home";
        //        IndexNode.Action = "javascript:openPage('/umbraco/backoffice/Plugins/AgentAdmin/[function]');";
        //        IndexNode.Icon = "icon-home";
        //        IndexNode.HasChildren = false;
        //        IndexNode.Menu = new List<IAction>();
        //        OnBeforeNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
        //        if (IndexNode != null)
        //        {
        //            tree.Add(IndexNode);
        //            OnAfterNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
        //        }
    
        //        var UsersNode = XmlTreeNode.Create(this);
        //        UsersNode.NodeID = "1";
        //        UsersNode.NodeType = "Users";
        //        UsersNode.Text = "Users";
        //        UsersNode.Action = "javascript:openPage('/umbraco/backoffice/Plugins/AgentAdmin/[function]');";
        //        UsersNode.Icon = "icon-users";
        //        UsersNode.HasChildren = false;
        //        UsersNode.Menu = new List<IAction>();
        //        OnBeforeNodeRender(ref tree, ref UsersNode, EventArgs.Empty);
        //        if (UsersNode != null)
        //        {
        //            tree.Add(UsersNode);
        //            OnAfterNodeRender(ref tree, ref UsersNode, EventArgs.Empty);
        //        }
    
    
        //    }
    
    //    public override void RenderJS(ref System.Text.StringBuilder Javascript)
    //    {
    //        Javascript.Append(
    //           @"function openPage(url) {
    //             UmbClientMgr.contentFrame(url);
    //            }");
    //    }
    //}
    

    }

    StartUpHandler.cs

    using System.Web.Routing;

    using Umbraco.Core;

    namespace YourProject.AppPlugins.AgentAdmin.AppStart {

    public class StartUpHandlers : ApplicationEventHandler {

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
            RouteConfig.RegisterRoutes(RouteTable.Routes);
    
        }
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft