LiamB
LiamB

Reputation: 18586

<%= How to write out a String MVC.Net

In ASP.NET MVC, if I have some content on the page I can do the following:

<%=Html.ActionLink(..Blah Blah..)%>

How can I acheive the same result in the following block:

if(a==b)
{
   Html.Encode("output some text here");
}

I want to do this without a lot of tags, hence why I am asking.

Upvotes: 0

Views: 2004

Answers (6)

Jace Rhea
Jace Rhea

Reputation: 5018

<%= a==b ? Html.Encode("output some text here") : string.Empty %>

Upvotes: 4

Adel
Adel

Reputation: 1468

in MVC 4, simply use the following:

 @if (x == y)
       {
         @Html.Encode('This is Just text')
       }

Upvotes: 0

Geoff
Geoff

Reputation: 4746

<% if(a==b) {
  Response.Write(Html.Encode("output some text here"));
}%>

Upvotes: 4

giorgian
giorgian

Reputation: 3825

<%= ... %> is just a shortcut for <% Response.Write(...); %>.

Upvotes: 5

Jon Benedicto
Jon Benedicto

Reputation: 10582

To do this, you need to "drop" out of code and into the markup by closing the code with %> and then restarting the code block after your text with <%

For example:

if (a == b)
{
    %>output some text here<%
}

Upvotes: 0

Mouk
Mouk

Reputation: 1737

 <% If(a==b){%>
   <% = Html.Encode("output some text here");%>    
 <% }%>

Upvotes: 0

Related Questions