Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Html.Raw to work within an if statement in MVC Razor Javascript?

I've been trying to get a dynamic button slider, but I can't even get a simple conditional to print anything. I thought @Html.Raw was the best way to print HTML from within Razor. Am I missing something? Document.write isn't even recognized. Any thoughts on how I should pass static HTML here, yet alone page variables?

 <div class="content">
                @{var localselected = 1;

                    if (localselected == 1){
                        Html.Raw("test1");
                    }
                    else if (localselected == 2){
                        Html.Raw("test2");
                        Html.Raw(page2);
                   }
               }
            </div>
like image 410
iontom Avatar asked Nov 01 '22 06:11

iontom


2 Answers

You just need to wrap the localselected variable in @{}, like below:

   @{var localselected = 1;}

   @if (localselected == 1)
   {
       @Html.Raw("test1")
   }
   else if (localselected == 2)
   {
       @Html.Raw("test2")
       @Html.Raw(@Html.Partial("Page")).ToString()
   }

Note: Page is a parital view. using @Html.Raw(@Html.Partial("Page")).ToString() it'll print out all of html elements and content.

like image 75
Lin Avatar answered Nov 13 '22 23:11

Lin


There are two options available to print content from server code inside Razor view

  1. @: text
  2. Sample text

Please refer http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

like image 38
Thanigainathan Avatar answered Nov 13 '22 22:11

Thanigainathan