Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 09:56
    Simon Dingley
    0

    Render Form from within own Razor File

    I'm trying to render a Contour form from inside of my own Razor macro but keep getting a nul reference exception. The same form works on pages using the existing Razor macro directly so not sure what I'm doing wrong?

    I've simplified the call to the following:

                        @{
                            string action = "ContourForm";
                            string controller = "FormRender";
                            string formtoken = "UmbracoContourForm";
                            string formGuid = "fe39ea42-1ae3-4991-9fd9-b3d7e9d5ec36";
    
                            @Html.RenderMvcAction(action, controller, formGuid, formtoken)
    
                        }

    Am I missing something? Contour 3 and Umbraco 4.11.4.

    Thanks, Simon

  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 10:05
    Simon Dingley
    0

    Stack trace in case it helps:

       at CallSite.Target(Closure , CallSite , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
       at Umbraco.Forms.Mvc.Controllers.FormRenderController.ContourForm() in c:\Program Files (x86)\teamcity\buildAgent\work\fdc2f0fcbbcef310\Umbraco.Forms.Mvc\Controllers\FormRenderController.cs:line 30
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  • Jeroen Breuer 4866 posts 12165 karma points MVP 3x admin c-trib
    Mar 20, 2013 @ 10:12
    Jeroen Breuer
    0

    Did you try to call it as a macro inside your razor macro? Something like this:

    @Umbraco.RenderMacro("myMacroAlias", new { name = "Ned", age = 28 })

    More info here: http://umbraco.com/follow-us/blog-archive/2012/10/30/getting-started-with-mvc-in-umbraco-410.aspx

    Jeroen

  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 10:25
    Simon Dingley
    0

    I get a "The type or namespace name 'RenderMacro' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)" exception on doing that - perhaps because the siite is not in "MVC Mode"? The site is not MVC and so not sure what the implications of turning this feature on are?

    Cheers, Simon

  • Jeroen Breuer 4866 posts 12165 karma points MVP 3x admin c-trib
    Mar 20, 2013 @ 10:27
    Jeroen Breuer
    0

    My example is for MVC so that won't work. You could try the Razor Components package to render the macro instead: http://our.umbraco.org/projects/website-utilities/razor-components

    Jeroen

  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 10:49
    Simon Dingley
    0

    As a last resort I might try the third party package however it would be good to know why this can't work out of the box, the code was copied directly from the MvcRenderContourForm.cshtml razor file and that works fine..

    Thanks

  • Comment author was deleted

    Mar 20, 2013 @ 10:53

    Well the macro needs the param formGuid it's fetches it from there, the string formGuid isn't used

  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 11:06
    Simon Dingley
    0

    So I have to use a RenderMacro helper of some sort? Is there any way of me adding the parameter inline?

    Thanks Tim

  • Comment author was deleted

    Mar 20, 2013 @ 11:12

    No you just need to add a param to your macro

  • Simon Dingley 1444 posts 3372 karma points c-trib
    Mar 20, 2013 @ 11:44
    Simon Dingley
    0

    It's a bit more complicated than that as the macro will be accepting multiple form guids and using some internal logic to make the selections which is why I am trying to render the macro directly from within my own. Guessing I'm going to have to perhaps rethink this however it would be good if this could be considered for a future release?

    Simon

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Apr 18, 2013 @ 07:55
    Tom Fulton
    100

    Ran into the same problem here - trying to render the Razor macro from Webforms.  I wasn't sure if there was some RenderMacro helper we can use in Webforms (other than umbraco.library.RenderMacroContent -- will that work?).  So I was able to work around it like so:

    @helper RenderContourForm(string formGuid) {
       string action = "ContourForm";
       string controller = "FormRender";
       string formtoken = "UmbracoContourForm";
    // We need to build up an object with the formGuid, as the controller doesn't seem to read it from the formGuid variable but the latest parameter
       dynamic p = new ContourFormParameters();
        p.formGuid = formGuid;

      @Html.RenderMvcAction(action, controller, formGuid, formtoken, (umbraco.MacroEngines.DynamicNode)Model, (DynamicObject)p);
    }

    ContourFormParameters is just a quick class I stuck in /App_Code/ so we have an object to assign the formGuid parameter to:

       public class ContourFormParameters : DynamicObject
       {
           public string formGuid { get; set; }
       }

    Then you can just call @RenderContourForm("") to insert the form.

    Hope this might help someone.  Should there be an easier way to dynamically insert a Razor form from an "old Razor" file (macroScripts)?

    -Tom

  • Mauricio 22 posts 86 karma points
    Oct 23, 2015 @ 16:39
    Mauricio
    0

    Hi Tom!

    Sorry I am trying to do that but I do not understand what I need to do, where I need to create that code. I am trying to convert this code to MVC umbraco 7.3 with PartialView:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @using Umbraco.Forms.Mvc.Bridge.Html;

    @{

    string action = "ContourForm";
    string controller = "FormRender";
    string formtoken = "UmbracoContourForm";
    string formGuid = CurrentPage.MacroParameters["FormGuid"];
    
    @Html.RenderMvcAction(action, controller, formGuid, formtoken, (umbraco.MacroEngines.DynamicNode)Model, (System.Dynamic.DynamicObject)Parameter);
    

    }

    Could you guide me a bit more?

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft