c# - Switch statement inside Razor CSHTML -


i'm developing project in asp.net mvc4, twitter.bootstap 3.0.0 , razor. in view, need display buttons depending of property value. using switch statement, example below doesn't work (nothing 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 bit, using <span> tag, 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 explain why?

thanks.

it's funkiness of razor. when you're in normal html , use c# code, putting @ symbol on write result page:

<p>@html.actionlink("whatever", "whatever"...)</p> 

this similar old-school <%= %>.

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

however, html.actionlink method returns mvchtmlstring object in .net world. in first example, you've got regular c# code block. calling html.actionlink() there executes , returns mvchtmlstring nobody. in second example, you've jumped html context, writes html again.

you can use special <text> block html instead of using <span> or other real html, , write directly without writing html:

case enums.stage.readytostart:     <text>@html.actionlink(language.start, "start", new { id=model.processid }, new { @class = "btn btn-success" })</text>     break; 

you can use 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 both here

edit

actually, in case, don't need either one. need @ symbol, enough html:

case enums.stage.readytostart:     @html.actionlink(language.start, "start", new { id=model.processid }, new { @class = "btn btn-success" })     break; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -