Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Razor HTML Partial View not rendering between 'If' Block?

I am trying to render a Razor html Partial view based on a ViewBag condition, but I always get compliation errors.

 @{

     if (ViewBag.Auth)
     {
        @Html.RenderPartial("_ShowUserInfo")
     }

 }

I also tried...

 @if (ViewBag.Auth)
 {
    @Html.RenderPartial("_ShowUserInfo")
 }

Error message:
Compiler Error Message: CS1502: The best overloaded method match for                  
'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' 
has some invalid arguments
like image 296
Sumit Chourasia Avatar asked Jan 11 '23 22:01

Sumit Chourasia


2 Answers

You need to cast ViewBag.Auth to boolean

 @if ((bool)ViewBag.Auth)
 {
    @{ Html.RenderPartial("_ShowUserInfo");  } 
 }

Also you need to use @{ } syntax with RenderPartial

like image 167
Satpal Avatar answered Jan 22 '23 11:01

Satpal


Try using like this..

@if (ViewBag.Auth)
 {
    @{ Html.RenderPartial("_ShowUserInfo") }
 }
like image 40
Manoj Avatar answered Jan 22 '23 11:01

Manoj