2

in ASP.NET MVC i was trying to bind a model field with displayText with code below:

<%    if (Model.WillAttend == true)
           Html.DisplayTextFor(x => x.Name);   %>

but when i tried:

<%    if (Model.WillAttend == true)     %>  
         <% = Html.DisplayTextFor(x => x.Name)     %>

it is working, why? both seems same code, the only different is in below one is just each line is separated with the server side tag.

1 Answer 1

2

The difference is in the = sign after the open tag <%. This ensures that the value is written to the output. The first example is simply declaring a value and not doing anything with it.

Check out this blog entry for more info on the ASPX view engine tag syntax.

This is simplified a lot with Razor syntax, where you are able to just prefix a line in a codeblock with @ in order to write it to output. I don't know if there is a similar functionality in the ASPX view engine though.

Sign up to request clarification or add additional context in comments.

5 Comments

so u mean we cant write all the c# code within a single server side tag (<% %>) ?
Not a single one no, not that I can find at least (I've only really worked with the Razor syntax). Code that you want to output needs some way of writing it to the output, hence the <%= syntax.
You can declare a string though, and just output that in the end, but I'm not sure that would be easily readable.
there is no "classic" MVC, there are different View Engines. codeproject.com/Articles/467850/ASP-NET-MVC-view-engines
@RobertP. ASPX, that's the word i was looking for. Thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.