Copied to clipboard

Flag this post as spam?

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


  • Damian Green 452 posts 1433 karma points
    Jun 20, 2012 @ 18:48
    Damian Green
    0

    Cant access/use Html.Raw in macro

    Going mad here and dont know whats heppened but umbraco doesnt recognise Html.Raw,

    I get 

    Object reference not set to an instance of an object.

    when i try something simple like @Html.Raw("<p>test</p>)

    I think im doing something daft and cant see for looking at it.  

    Any ideas whats happened?

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 20, 2012 @ 18:59
    Fuji Kusaka
    0

    Hi Damian,

    Am new to Razor was well, could you try doing something like

    @Html.Raw(message)

    and call you message after

    @message("test")
  • Douglas Ludlow 210 posts 366 karma points
    Jun 20, 2012 @ 19:36
    Douglas Ludlow
    1

    @Fuji, it doesn't work like that.

    @Damian, what error are you getting (assuming its more than just "Error loading Razor Script")? If it isn't more than "Error loading Razor Script" then try wrapping your script in a try/catch and write out the exception:

    @try
    {
       
    @Html.Raw("<p>test</p>")
    }
    catch(Exception e)
    {
       
    @e.ToString()
    }

    Also in the example you gave (@Html.Raw("<p>test</p>)), you are missing a double quote after the closing p tag, that would cause issues if that's how you have it in the code.

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 20, 2012 @ 19:52
    Fuji Kusaka
    0

    Hi Douglas,

    Yes true, am using it this way though,

    @helper Message(String message)
    {  
     @Html.Raw(message)
    }

     

    //fuji

  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 10:15
    Damian Green
    0

    Hi,

    Im not new to razor thats why its bugging me. 

    Missing the closing quote was a typo as i just typed it out.

    The error im getting is in my first post:

    Object reference not set to an instance of an object.

    Also when i use visual studio @Html is not listed in the intellisense.

    Ive used it tons of times before but its just vanished! Very odd.

    Its like there is a namespace issue somewhere.

  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 10:53
    Damian Green
    0

    Ok ive dissected things and isolated the fault and it doesnt look like its Html.Raw.  Should have done this yesterday but it was odd that everything was working untl i inserted the Html.Raw line.

    I have now cut my razor macro right down to this and it works. Note the InitializePage() override that i am using but ive stripped out all code for the example.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    <h1>Testing Raw</h1>

    @functions {
         
      protected override void InitializePage()
      {              
          
      }    
                
    }

    As soon as i insert the Html.Raw it breaks:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    <h1>Testing Raw</h1>

    @Html.Raw("

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    <h1>Testing Raw</h1>

    @Html.Raw("<h2>raw output</h2>")

    @functions {
         
      protected override void InitializePage()
      {              
          
      }    
                
    }

    However, it also works if i remove the InitializePage override so it looks like its that causing the issue.

    I have used this override a lot previously so i know it works but something is not right!

    Any ideas? Im now going to triple check this override.


  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 10:57
    Damian Green
    0

    Arghh! Everytime i edit the post it stuffs up the code ive inserted! Another forum bug. :(

    Anyway the bottom code segment shown breaks but if i remove either the @Html.Raw or the overrride from the functions it works.

     

  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 11:03
    Damian Green
    0

    Cripes!

    Looked at some examples and i was missing:

    base.InitializePage();
    

    If i add that into the override it works.

    The VERY SCARY part of this is I have sites running live that dont have this line in the override and they are working spot on!

    Can someone elighten me as to what is it causing the issue because im worried about the other scripts ive got running but obviously dont want to mess with them as they have been working for months.  Need to make sure they are ok its its not just a fluke they are running! 

     

     


  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 11:07
    Damian Green
    0

    And Fuji - not sure why you have got that helper message as you have basically written a method that just wraps the Html.Raw so it basically just changes the name you are calling.

    No need for it - you can just do @Html.Raw(string or string variable)

     

  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 11:20
    Damian Green
    0

    If i use just a variable from the page it works fine so it seems to be something that doesnt get setup for Html.Raw if the base class is not inititalized.

    <h1>Testing Raw</h1>

    <h2>@sVal</h3>

    @functions {
      
      string sVal "Hello";
                
      protected override void InitializePage()
      {              
         
      }    
                
    }
  • Damian Green 452 posts 1433 karma points
    Jun 21, 2012 @ 11:42
    Damian Green
    0

    Had a hunt around the source code but because im unfamiliar with it I cant even find the correct class where the base page for a front end page is.

    I'll leave this unsolved for a little bit to see if anyone can aid my understanding of whats going on here.

    Thanks

     

  • Damian Green 452 posts 1433 karma points
    Jun 27, 2012 @ 14:23
    Damian Green
    0

    Nobody?

    I'll mark as solved but still would like to know whats happening here.

     

  • ljcooper 21 posts 86 karma points
    Jul 17, 2012 @ 20:36
    ljcooper
    0

    I just had the same issue and found this thread (your solution worked, btw), I'd be interested if anyone has an explanantion as well.

  • Damian Green 452 posts 1433 karma points
    Jul 18, 2012 @ 10:13
    Damian Green
    0

    Yes its a weird one.

    My guess is the HTML object gets initialised in the page initialisation event and if you override it it you must remember to call the base method.

    Hence if you dont thats why you get the object reference not set error.

     

Please Sign in or register to post replies

Write your reply to:

Draft