Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.Partial not working under if statement

I have following loc in one of my view pages:

@* Html.Partial("Form")*@

  @{
    var role = Convert.ToInt32(Session["Role"]);
    if (role == 2)
    {
        Html.Partial("Form");
    }
}

The Html.Partial("Form") works fine when its outside any if statement and renders everything fine.

But when Inside an if block it renders nothing, if statements is hit, it’s true, debugger eves reads the function and goes to the Form Partial view and goes through every line in it but at the end there is not output on the page.

Kindly help

like image 760
Maven Avatar asked Mar 29 '13 08:03

Maven


1 Answers

You should use RenderPartial method when you are inside a code block.

Html.RenderPartial("Form");

Html.Partial returns an HtmlString which would be rendered to the page if it wasn't inside a code block. In your case Razor parses your view and returns the result to your code. Since you don't do anything to render it, you don't get the output.

like image 181
Ufuk Hacıoğulları Avatar answered Oct 11 '22 11:10

Ufuk Hacıoğulları