2

I'm in trouble here. In my view i have the following code:

@if (!string.IsNullOrEmpty(alert)) { 
    <div class="alert">@Html.Raw(alert)</div>
}

But an exception of type NullReferenceException is being thrown saying that the Html property is null. How i can solve this?

OBS: My view page inherits from a class that by their turn inherits from System.Web.WebPages.WebPage

// WEBCONFIG

<system.web>
<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.WebPages.Deployment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>
<httpRuntime targetFramework="4.0"/>

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
mnatan.brito
  • 883
  • 5
  • 18
  • 32

1 Answers1

5

I didn't get the solution, so i changed the code to:

@if (!string.IsNullOrEmpty(alert)) {    
    <div class="alert">@(new HtmlString(alert))</div>
}

Razor doesn't encode HtmlString. So it worked fine.

mnatan.brito
  • 883
  • 5
  • 18
  • 32