13

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

Maven
  • 14,587
  • 42
  • 113
  • 174

2 Answers2

31

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.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
5

Try replacing:

Html.Partial("Form"); // <- this will return string

with:

Html.RenderPartial("Form"); // <- writes to response

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

SharpC
  • 6,974
  • 4
  • 45
  • 40
webdeveloper
  • 17,174
  • 3
  • 48
  • 47