asp.net mvc razor - 如何打破C#

时间:2021-12-09 11:50:22

If I'm working on a view in Razor, and I'm currently in a code block and want to output something, how do I do this? To illustrate my question, I'm using echo from PHP below:

如果我正在使用Razor中的视图,并且我目前处于代码块中并希望输出内容,我该如何做?为了说明我的问题,我在下面使用PHP的echo:

<p>
  @if (Model.NumberOfWidgets > 100)
  {
    echo(Model.NumberOfWidgets);
  }
  else
  {
    echo("There are loads of widgets.");
  }
</p>

So I'm using echo where I want to tell Razor that I'm not doing C# anymore, I'm meaning this should be written to the output. How do I do this?

所以我使用echo我想告诉Razor我不再做C#了,我的意思是这应该写入输出。我该怎么做呢?

Edit: I tried Response.Write, but that gets written before the view markup, at the top of the page!

编辑:我尝试了Response.Write,但是在页面顶部的视图标记之前写了!

2 个解决方案

#1


13  

<p>
  @if (Model.NumberOfWidgets > 100)
  {
    @Html.DisplayFor(m => m.NumberOfWidgets)
  }
  else
  {
    @:There are loads of widgets  //or <text>Thera are loads of widgets</text>
  }
</p>

#2


4  

Begin your line with @: this will tell Razor that it's actually ouput that you want to show and not C# code.

开始使用@:这将告诉Razor它实际上是你要显示的输出,而不是C#代码。

<p>
  @if (Model.NumberOfWidgets > 100)
  {
    @: @Model.NumberOfWidgets
  }
  else
  {
    @: There are loads of widgets.
  }
</p>

#1


13  

<p>
  @if (Model.NumberOfWidgets > 100)
  {
    @Html.DisplayFor(m => m.NumberOfWidgets)
  }
  else
  {
    @:There are loads of widgets  //or <text>Thera are loads of widgets</text>
  }
</p>

#2


4  

Begin your line with @: this will tell Razor that it's actually ouput that you want to show and not C# code.

开始使用@:这将告诉Razor它实际上是你要显示的输出,而不是C#代码。

<p>
  @if (Model.NumberOfWidgets > 100)
  {
    @: @Model.NumberOfWidgets
  }
  else
  {
    @: There are loads of widgets.
  }
</p>