MVC 3 Razor Syntax for straight text output?

后端 未结 3 1764
北荒
北荒 2021-02-06 20:33

Using Razor how/can you write straight text with out wrapping it in some type of html tag?

Example (This works but adds extra span tags):

@{ var foo = tr         


        
相关标签:
3条回答
  • 2021-02-06 20:52

    use the <text> tags

    @{ var foo = true; }
    @if(foo) { <text>Yes</text> } else { <text>No</text> }
    

    The <text> tag signals to the razor view engine to write the contents to the output.

    Alternatively, you can use @:

    @{ var foo = true; }
    @if(foo) { @:Yes } else { @:No }
    
    0 讨论(0)
  • 2021-02-06 21:02

    A point worth to be noted here:

    @: can be used only inside an @

    (in case any body like me is wondering why @: does not work!)

    0 讨论(0)
  • 2021-02-06 21:07

    If you're trying like I am to inject data into Javascript I found this to work.

    @Html.Raw(table)
    

    I was pulling data from a database and injecting it into the code for a Google chart.

    So in the OP's case,

    @if (foo) { Html.Raw("Yes") } else { Html.Raw("No") }
    
    0 讨论(0)
提交回复
热议问题