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
3 Answers
Reset to default 2You 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
- @: text
- 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>