Switch statement inside Razor CSHTML

34,625

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:

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

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

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

However, the Html.ActionLink method simply returns an MvcHtmlString object 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 MvcHtmlString to nobody. In your second example, you've jumped back into the HTML context, so it writes the HTML again.

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:

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:

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

Related videos on Youtube

Leonel Sanches da Silva
Author by

Leonel Sanches da Silva

Programmer, Systems Analyst, IT Architect and Entrepreneur from Curitiba, Brazil, living in Irvine, California. Bachelor in Computing Science from Federal University of Paraná since 2007, acting in IT since 1998 as a hobbist. Microsoft MVC 2017-2018. I own a company named Design Líquido (Liquid Design, in english), which provides services in Graphical Design and IT (http://www.designliquido.com.br), and I helped to found a T-Shirt and Clothing online store (http://doppelstore.com.br) and a Benefits company (https://useswood.com), and more companies should be on inception shortly. Additionally, I'm a professional tutor, specialized in professional disciplines for IT professionals in Brazil: http://codingcraft.com.br. GitHub: https://github.com/leonelsanchesdasilva NuGet: https://www.nuget.org/profiles/cigano/ Liked my answers? Did I help you? Buy me a beer (or, in Portuguese, me paga uma bera).

Updated on October 07, 2020

Comments

  • Leonel Sanches da Silva
    Leonel Sanches da Silva over 3 years

    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 switch statement, the example below doesn't work (nothing is displayed):

    @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:

    @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.

  • Leonel Sanches da Silva
    Leonel Sanches da Silva over 10 years
    I liked most of the last example. Thanks!
  • Doug
    Doug over 2 years
    I never knew about the @: syntax. Very helpful!