C# Razor CSHTML 中的 Switch 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19213342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:25:07  来源:igfitidea点击:

Switch statement inside Razor CSHTML

c#asp.netasp.net-mvcasp.net-mvc-4razor

提问by Leonel Sanches da Silva

I'm developing a project in ASP.NET MVC4, Twitter.Bootstap 3.0.0 and Razor. In a View, I need to display buttons depending of a property value. Using the switchstatement, the example below doesn't work (nothing is displayed):

我正在 ASP.NET MVC4、Twitter.Bootstap 3.0.0 和 Razor 中开发一个项目。在视图中,我需要根据属性值显示按钮。使用该switch语句,下面的示例不起作用(不显示任何内容):

@switch (Model.CurrentStage) { 
    case Enums.Stage.ReadyToStart:
        Html.ActionLink(Language.Start, "Start", new { id=Model.ProcessId }, new { @class = "btn btn-success" });
        break;
    case Enums.Stage.Flour:
        Html.ActionLink(Language.GoToFlour, "Details", "Flours", new { id=Model.Flour.FlourId }, new { @class = "btn btn-success" });
        break;
    ...
}

Changing a bit, using a <span>tag, the code works:

稍微改变一下,使用<span>标签,代码有效:

@switch (Model.CurrentStage) { 
    case Enums.Stage.ReadyToStart:
        <span>@Html.ActionLink(Language.Start, "Start", new { id=Model.ProcessId }, new { @class = "btn btn-success" })</span>
        break;
    case Enums.Stage.Flour:
        <span>@Html.ActionLink(Language.GoToFlour, "Details", "Flours", new { id=Model.Flour.FlourId }, new { @class = "btn btn-success" })</span>
        break;
    ...
}

Can someone explain why?

有人可以解释为什么吗?

Thanks.

谢谢。

采纳答案by Joe Enos

It's the funkiness of Razor. When you're in normal HTML and use C# code, putting something with an @symbol on it will write the result to the page:

这就是 Razor 的时髦之处。当您使用普通 HTML 并使用 C# 代码时,@在其上放置带有符号的内容会将结果写入页面:

<p>@Html.ActionLink("whatever", "whatever"...)</p>

This is similar to the old-school <%= %>.

这类似于老派<%= %>

<p><%= SomeMethodThatReturnsSomethingThatWillBeWritten() %></p>

However, the Html.ActionLink method simply returns an MvcHtmlStringobject in the .NET world. In your first example, you've got a regular C# code block. So calling Html.ActionLink()from there simply executes it and returns the MvcHtmlStringto nobody. In your second example, you've jumped back into the HTML context, so it writes the HTML again.

但是,Html.ActionLink 方法只是返回MvcHtmlString.NET 世界中的一个对象。在您的第一个示例中,您有一个常规的 C# 代码块。所以Html.ActionLink()从那里调用只是执行它并将返回MvcHtmlString给nobody。在您的第二个示例中,您已跳回 HTML 上下文,因此它再次编写 HTML。

You can use the special <text>block to get back to HTML instead of using <span>or other real HTML, and it will write directly without writing the extra HTML:

您可以使用特殊<text>块返回 HTML 而不是使用<span>或其他真正的 HTML,它会直接编写而无需编写额外的 HTML:

case Enums.Stage.ReadyToStart:
    <text>@Html.ActionLink(Language.Start, "Start", new { id=Model.ProcessId }, new { @class = "btn btn-success" })</text>
    break;

You can also use the similar @:syntax:

您还可以使用类似的@:语法:

case Enums.Stage.ReadyToStart:
    @:@Html.ActionLink(Language.Start, "Start", new { id=Model.ProcessId }, new { @class = "btn btn-success" })
    break;

You can read more about both here

您可以在此处阅读有关两者的更多信息

EDIT

编辑

Actually, in this case, you don't need either one. You just need the @symbol, which will be enough to get you back into the HTML:

实际上,在这种情况下,您不需要任何一个。你只需要这个@符号,它就足以让你回到 HTML 中:

case Enums.Stage.ReadyToStart:
    @Html.ActionLink(Language.Start, "Start", new { id=Model.ProcessId }, new { @class = "btn btn-success" })
    break;