简体   繁体   English

如何使用在WebMatrix(C#Razor语法)中创建的帮助器?

[英]How can I use the helper I created in WebMatrix (C# Razor syntax)?

I am trying to use WebMatrix to create static html. 我正在尝试使用WebMatrix创建静态html。 (Think CMS.) (请考虑CMS。)

I have this helper in App_Code/CardHelpers.cshtml 我在App_Code / CardHelpers.cshtml中有此帮助器

@helper Cards (string mysuit){

// Class Tags
var ss = Html.Raw("<span class = \"spade\">"); 
var sh = Html.Raw("<span class = \"heart\">");  
var se = Html.Raw("</span>");

// Suits
var S = Html.Raw(ss + "&spades;" + se); 
var H = Html.Raw(sh + "&hearts;" + se);

<p> @mysuit and @H</p>

}

I call it with 我用

@CardHelpers.Cards("S")

The static html output is 静态html输出是

<p> S and <span class = "heart">&hearts;</span></p>

So I can use @H inside the helper to create the html I want, but how can I pass in a suit (such as "S") and create the appropriate html. 因此,我可以在助手中使用@H来创建所需的html,但是如何传递西装(例如“ S”)并创建适当的html。 Here, I just get back the S, but what I want to return is 在这里,我刚刚取回S,但是我想返回的是

<span class = "spade">&spades;</span>

The whole point of Razor is that you can mix markup and C# syntax. Razor的全部要点是您可以混合使用标记和C#语法。 So what you want to do is put a conditional or switch statement in that selects the correct output for the given input, like this: 因此,您要执行的操作是在条件语句或switch语句中放入为给定输入选择正确的输出,如下所示:

@{ 
  string result = "";
  switch(mysuit) { 
    case "H": result = H; break;
    case "S": result = S; break;
    default: break;
  }
  <p> @mysuit and @result</p>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM