最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论