3,razor and HTML mixed output traps and tricks
Property name MiscalculationSometimes we have to make the HTML content with the Razor statement immediately, eg: Hello, Mr. A. Suppose the variable name is viewbag.name, hello, @ViewBag. Name//So the output is only "Hello," and the latter name is treated as a ViewBag attribute went, so if you want to solve this problem is
Add a parenthesisGood, such as "Hello, (@ViewBag. Name)" and one more way
Wrap it up with an HTML tag, such as "Hello,< tags > @ViewBag. name</tag > sir", or to wrap it up with HTML tags.
Unexpected extra attributesIf you want to output a C # namespace, and the output part of the north wing to become a razor variable, the category is System.Web.Mvc.Controller eg:@{viewbag.mvcnamespace= "SYSTEM.WEB.MVC";} Class is @viewbag.mvcnamespace.controller the result output throws an error, and the string does not contain the controller definition because razor resolves @ When viewbag.mvcnamespace the controller as a controller property of the @viewbag.mvcnamespace, parsing the error, the solution to this situation
is to add a parenthesis,Eg: @ (viewbag.mvcnamespace). Controller, it will output "SYSTEM.WEB.MVC"
output email address with @ Hop-off sub-element< hyperlink a href= "[email protected]" >aaa
Add a parenthesisWe can,
output a string without HTMLEncodePresets use razor syntax to output variables, all content presets will be HTML encoded as HTMLEncode, this is to protect the Web page against cross-site scripting attacks, with this default, to ensure that the novice write unsafe code eg:@{viewbag.description= " < label style= ' font-size:12px; ' > Description Text </label > ";} @ViewBag. Description The result of the final output is a HTMLEncode version: the greater-than-sign double quotation marks have become the form of & plus some letters, and the solution is to use the @html.raw helper method to help us output the content
@Html. Raw(viewbag.description) This will be the same as the output we want to look like.
Razor and HTML mixed output traps and tricks