Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC @helper error with HTML.Raw

I have the following code in a Razor Helper file

@helper MakeNoteBlank(string content)
{
    string msg = "";

    if(content == null)
    {
        msg = " ";
    }
    else
    {
        msg = content;
    }

    <div class="note">
        <p>
             @Html.Raw(msg)
        </p>
    </div>
}

The code fails at execution with the @Html.Raw(..) statement, stating that "Object reference not set to an instance of an object."

If I remove the @Html.Raw(..) and output 'msg' directly then there is no problem.

What am I doing wrong?

like image 879
BENBUN Coder Avatar asked Feb 17 '15 23:02

BENBUN Coder


1 Answers

use @(new HtmlString()) instead of @Html.Raw()

like image 157
Ravi Rajan Avatar answered Nov 06 '22 06:11

Ravi Rajan