jquery - Getting Html.Raw to work within an if statement in MVC Razor Javascript? - Stack Overflow

I've been trying to get a dynamic button slider, but I can't even get a simple conditional to

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>

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>
Share Improve this question asked Jan 30, 2014 at 18:16 iontomiontom 4732 gold badges6 silver badges15 bronze badges 1
  • Is localselected Razor variable? – Satpal Commented Jan 30, 2014 at 18:23
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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

  1. @: text
  2. Sample text

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

You should not need to wrap the whole thing in @{ ... }. Try this instead:

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

    if (localselected == 1) {
        @Html.Raw("test1")
    }
    if (localselected == 2) {
        @Html.Raw("test2")
        @Html.Raw(page2)
    }
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745431971a4627432.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信