asp.net mvc - What are the rules for nesting @'s in Razor syntax? -
asp.net mvc - What are the rules for nesting @'s in Razor syntax? -
i've started razor (v3) , it's fine, don't understand rules behind when need utilize @ sign.
here illustration current project.
in code:
@if (viewbag.forcepublic == null && user.identity.getuserid() == @model.seller.id) { <h2>listing status</h2> switch (model.status) { if set @ in front end of switch statement, runtime error saying don't need nest @ signs.
however, in code:
@if(!model.bizaddress.streetaddress.isempty()) { @html.displayfor(model => model.bizaddress.streetaddress); } not not syntax error (as 1 expect, considering illustration above), if don't include @ sign in front end of html.displayfor, streetaddress not printed. instead silently fails!
i understand why happening can avoid subtle bugs might cause in future.
i suspect root cause has fact templating (i think) analogous preprocessor step, , scoping of c# code known @ runtime, these 2 examples structurally same after both steps take place (preprocess, compile , run).
@ in razor used 2 different purposes:
1- denote start of code block razor using @ character. <% %> in web forms.
2- inline expressions (variables , functions) start @ (for writing content on page). <%: %> or <% response.write() %> in web forms.
in code, @ character before if denotes start of code block. end of block end } of if block. don't need , not allowed utilize @ in between. but, @ before html.displayfor() used inline expression. in fact, should not set semi-colon @ end of line, otherwise semi-colon displayed after display name on page.
look @ quick reference more details.
asp.net-mvc razor
Comments
Post a Comment